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

Thread: WndProc break or return

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

    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,137

    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
    229

    Re: WndProc break or return

    Thank you.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

    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 12:58 AM.
    Best regards,
    Igor

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