-
July 11th, 2014, 08:06 AM
#1
WndProc break or return
In the Charles Petzold C++ book examples the cases in the switch (message) are returning return 0;
But if I create a project in Visual Studio 2010 it puts breaks; instead of return 0;
Whats up with that?
Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
July 11th, 2014, 08:29 AM
#2
Re: WndProc break or return
it doesn't matter whether you return or break ( and return later ) as long as you follow the documentation of the message regarding the expected return value and meaning. Maybe, the author of the visual studio project wanted to convey the idea that "most" messages expects a 0 return value "by default", hence the break, or he just sticked to the common (but not always sensible) practice of avoiding mlutiple function exit points.
Personally, I prefer Petzold's style though ...
-
July 11th, 2014, 08:57 AM
#3
Re: WndProc break or return
-
July 18th, 2014, 12:56 AM
#4
Re: WndProc break or return
Originally Posted by MasterDucky
In the Charles Petzold C++ book examples the cases in the switch (message) are returning return 0;
But if I create a project in Visual Studio 2010 it puts breaks; instead of return 0;
Whats up with that?
Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
The two fragments are semantically equivalent. Absolutely. You need to train yourself to "parse" code snippets on the fly.
Last edited by Igor Vartanov; July 18th, 2014 at 12:58 AM.
Best regards,
Igor
-
October 16th, 2014, 09:08 AM
#5
Re: WndProc break or return
return will actually return out of the function. the return value is important here. some, but not all need to return DefWindowProc.
break means that it will break out the switch clauses, so whatever code (important or not) will not be run if you return instead of break. Since this is a window procedure this might not be much of a problem. I believe the common practice is to stick with break most of the time, unless you want different return-values for different window messages.
(oh, now i see i am really really late. since this was posted in July. :P)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|