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;
}