WndProc break or return
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: WndProc break or return

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    234

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

  2. #2
    Join Date
    Oct 2008
    Posts
    1,164

    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 ...

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    234

    Re: WndProc break or return

    Thank you.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: WndProc break or return

    Quote Originally Posted by MasterDucky View Post
    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 01:58 AM.
    Best regards,
    Igor

  5. #5
    Join Date
    Feb 2009
    Posts
    22

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center