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 ...
Re: WndProc break or return
Re: WndProc break or return
Quote:
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.
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)