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;
}
}
5 replies to this topic
#1 Members - Reputation: 140
Posted 10 August 2012 - 07:54 PM
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?
Sponsor:
#2 Moderators - Reputation: 7667
Posted 10 August 2012 - 08:52 PM
You seem to have left out a break; just above case WM_CLOSE.
Maker of Machinery
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#3 Members - Reputation: 2108
Posted 10 August 2012 - 10:29 PM
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<wchar_t>! Mglui naflftagn std::codecvt<char,char,mbstate_t> eY'ha-nthlei!
#5 Members - Reputation: 2108
Posted 11 August 2012 - 10:35 PM
You cannot generally return 0. Look up the handled message and see what you should return (if you should do so).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.
Fruny: Ftagn! Ia! Ia! std::time_put_byname<wchar_t>! Mglui naflftagn std::codecvt<char,char,mbstate_t> eY'ha-nthlei!
#6 Moderators - Reputation: 6662
Posted 12 August 2012 - 12:23 AM
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.






