C/C++ Windows API

Moderators: Lundin
Number of threads: 450
Number of posts: 1225

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
SendMessage with LB_ADDSTRING help Posted by waggy55 on 9 Feb 2011 at 2:21 PM
Hi,
I'm having trouble displaying text to a listbox (I've included my code). I expected that when I press my left mouse button (on the MainWnd) the string "greeting" should display in the listbox. Instead, no text appears. The sendmessage function returns a non-error state (via the message box). I'm sure I'm just doing something stupid, but any help would really be appreciated.

Many thanks in advance!

P.S. I've included the whole C file, but the only relevant parts should be the "WM_LBUTTONDOWN" (near the top) and hMainWndEdit = CreateWindowEx (at the bottom)


#include <windows.h>
#include "resource.h"

#define RBGCOLOUR 255
#define GBGCOLOUR 255
#define BBGCOLOUR 255

HINSTANCE hInst;
HWND hMainWnd;
HWND hMainWndEdit;

const char *ClassName = "DialogueWindow";
const char *WindowName = "Dialogue Window";

char *greeting = "Lovely Text";

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* FUNCTION:	Window Procedure															*/
/* PURPOSE:		Process all messages that are intended for the app's window					*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
LRESULT CALLBACK WndProc(HWND hMainWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
    {

	case WM_SIZE:
        MoveWindow(hMainWndEdit, (0 + 20), (0 + 20), (LOWORD(lParam) - 40), (HIWORD(lParam) - 40), TRUE);
			
        break;

	case WM_LBUTTONDOWN:

		if(SendMessage(hMainWndEdit, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)greeting) == LB_ERR)
		{
			MessageBox(NULL, "LB_ERR", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		}
		else
		{
				MessageBox(NULL, "LB_GOOD", "Error!",
					MB_ICONEXCLAMATION | MB_OK);
		}
		
		break;

	case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDCLOSE:
                PostMessage(hMainWnd, WM_CLOSE, 0, 0);
				break;
        }
		break;

	case WM_CLOSE:

		DestroyWindow(hMainWnd);
        break;

	case WM_DESTROY:

        PostQuitMessage(0);
        break;

        default:
            return DefWindowProc(hMainWnd, msg, wParam, lParam);
    }
    return 0;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* FUNCTION:	WinMain																		*/
/* PURPOSE:		Initial Entry Point															*/
/*																							*/
/* RETURN VALUES: If the function succeeds, terminating when it receives a WM_QUIT message,	*/
/*				it should return the exit value contained in that message's wParam parameter*/
/*				If the function terminates before entering the message loop,				*/
/*				it should return zero.														*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
	/* DEFINITIONS */
    WNDCLASSEX wc;
	MSG Msg;
	RECT rMainWnd;

	hInst = hInstance;

	/*--------------------------------------------------*/
	/* STRUCTURE:	WNDCLASSEX							*/
	/* PURPOSE:		Contains Window struct. information	*/
	/*													*/
	/*--------------------------------------------------*/
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(0x000000);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU_MAINWND);
    wc.lpszClassName = ClassName;
    wc.hInstance     = hInstance;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	/*--------------------------------------------------*/
	/* FUNCTION():	RegisterClassEx						*/
	/* PURPOSE:		Registers window struct. for future */
	/*				use in CreateWindow					*/
	/*--------------------------------------------------*/
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	/*--------------------------------------------------*/
	/* FUNCTION():	CreateWindowEx						*/
	/* PURPOSE:		Creates Main Window					*/
	/*--------------------------------------------------*/
	hMainWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        ClassName,
        WindowName,
        WS_OVERLAPPEDWINDOW | ES_AUTOVSCROLL,
        CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
		NULL,
		hInstance,
		NULL);

    if(hMainWnd == NULL)
    {
        MessageBox(NULL, "Main Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	//Show window and update the window, to ensure it is properly drawn
    ShowWindow(hMainWnd, SW_SHOWMAXIMIZED);
    UpdateWindow(hMainWnd);

	/*--------------------------------------------------*/
	/* FUNCTION():	CreateWindowEx						*/
	/* PURPOSE:		Creates Edit Window					*/
	/*--------------------------------------------------*/
	GetClientRect(hMainWnd, &rMainWnd);  //Get size of MainWnd

	hMainWndEdit = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		"LISTBOX",
		NULL,		//Leave the control empty
		LBS_HASSTRINGS |
		WS_HSCROLL | WS_VSCROLL |
		WS_CHILD | WS_VISIBLE |
		ES_LEFT | ES_MULTILINE |
		ES_AUTOHSCROLL | ES_AUTOVSCROLL,
        (0 + 20), (0 + 20),
		(rMainWnd.right - 40), (rMainWnd.bottom - 40),
        hMainWnd,
        0,
        hInst,
        NULL);

	if(hMainWnd == NULL)
    {
        MessageBox(NULL, "Edit Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	//Show window and update the window, to ensure it is properly drawn
    ShowWindow(hMainWndEdit, SW_SHOW);
	UpdateWindow(hMainWndEdit);

	/*--------------------------------------------------*/
	/* FUNCTION():	GetMessage							*/
	/* PURPOSE:		Gets a message from app's message	*/
	/*				message queue.						*/
	/* TranslateM(): Any req'd additional processing	*/
	/* DispatchM(): Sends the message to the window it  */
	/*				was intended for					*/
	/*--------------------------------------------------*/
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Report
Re: SendMessage with LB_ADDSTRING help Posted by mh2x on 9 Feb 2011 at 7:53 PM
Hi,

Looking at the code, I see several general problems that I would recommend you to fix:

1) global variables like hMainWndEdit should be initialized to NULL.
2) Instead of creating the listbox like this after the main window, try to create child windows as part of the WM_CREATE message handling.

The problem here is that you create main window first, so ShowWindow & UpdateWindow API will call your window proc and access the uninitialized hMainWndEdit variable before it creates the child listbox.

3) Don't mix control styles. I see you have mixed LBS_HASSTRINGS with ES_MULTILINE --> ES_* is supposed to be for Edit controls, i.e. "EDIT" class and NOT "LISTBOX". Combining wrong values together result-in undesired behavior!

4) It looks like your listbox child window is covering most of the main window client area, so maybe you're not clicking on the main window. You might be clicking on the listbox instead!

I would try to add code after I created the listbox to populate it with some test strings first to see that it does exist and have strings in it...

I hope this helps!
Report
Re: SendMessage with LB_ADDSTRING help Posted by waggy55 on 10 Feb 2011 at 10:03 AM
Thanks a lot for your help! I've removed the edit controls and it works perfectly - sorry to waste your time with such a silly mistake!

I've taken on all your other points and implemented them, too. Thanks a lot for your advice - I'm sure it'll help me out in the future!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.