Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

SendMessage with LB_ADDSTRING help

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)


[code]
#include
#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;
}
[/code]

Comments

  • mh2xmh2x Posts: 1Member
    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!
  • waggy55waggy55 Posts: 4Member
    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!
Sign In or Register to comment.