Skip to main content
GameDev.net gamedev.net
🔒 Locked

Win32 Button Commands

Started by captacha Aug 11, 2012 at 1:54 AM 4 replies 1.5k views
Original Post
captacha
captacha
In my code the WM_COMMAND segment causes my program to exit when I click on a button. Why is this? Shouldn't it do nothing?

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
CreateWindow("button", "Button 1", WS_VISIBLE | WS_CHILD, (width/2)-40, (height/2)-12, 80, 25, hwnd, (HMENU)1, NULL, NULL);
CreateWindow("button", "Button 2", WS_VISIBLE | WS_CHILD, (width/2)-40, ((height/2)-12)-45, 80, 25, hwnd, (HMENU)2, NULL, NULL);
break;
case WM_COMMAND:
if(LOWORD(wparam) == 1)
{
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
break;
}
}
Endurion
Endurion
Not the problem here, but you don't return anything for any handled message. In most cases you also want to call DefWindowProc for handled messages.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
dilyan_rusev
dilyan_rusev
And also you should return 0 for handled messages. I don't know if this is the full code, but I can't see a "return 0" statement. Returning 0 is important.
Endurion
Endurion

And also you should return 0 for handled messages. I don't know if this is the full code, but I can't see a "return 0" statement. Returning 0 is important.

You cannot generally return 0. Look up the handled message and see what you should return (if you should do so).
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
SiCrane
SiCrane
In particular, a common mistake is to return 0 from WM_NCCREATE, which indicates that window creation shouldn't proceed and causes CreateWindow()/CreateWindowEx() to return a NULL handle. Confusingly, WM_CREATE does the opposite: 0 indicates success.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.