DefWindowProc

This is a discussion on DefWindowProc within the Windows Programming forums, part of the Platform Specific Boards category; Hi, What does DefWindowProc actually do? I mean, is it just a function which returns 0 for WM_QUIT and 1 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    DefWindowProc

    Hi,

    What does DefWindowProc actually do? I mean, is it just a function which returns 0 for WM_QUIT and 1 for all other messages, or does it actually do some processing?

    Is there somewhere where we could find out what DefWindowProc does for each individual message? For example if we had the source code for DefWindowProc, we could look and see:

    Code:
    LRESULT CALLBACK DefWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
     switch(uMsg)
     {
    ...
      case(WM_PAINT):
      //WHAT DOES MICROSOFT PUT HERE??
      break;
    ...
     }
     return 0;
    }
    I would just like to know so that when I write my own WndProc, I don't miss out on doing things which would have been done had I called DefWindowProc. Or should I be calling DefWindowProc in addition to my custom message handling I do in my WndProc procedure?

    Thanks in advance.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    DefWindowProc should be called in addition to your message handling, because it does tons of things for you. It does everything. Activates the window when you click on it in the taskbar, minimizes when the minimize button is pressed, lets you change the window size etc. It is something you should keep, because trying to handle all those things by yourself would be useless.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    Microsoft's use of DefWindowProc

    Quote Originally Posted by maxorator
    DefWindowProc should be called in addition to your message handling, because it does tons of things for you. It does everything. Activates the window when you click on it in the taskbar, minimizes when the minimize button is pressed, lets you change the window size etc. It is something you should keep, because trying to handle all those things by yourself would be useless.
    Hi,

    I am just looking at Microsoft's Window Procedure example from

    Platform SDK
    + User Interface Services
    + Windows User Interface
    + Windowing
    + Window Procedures
    + Window Procedures Overview
    + Using Window Procedures

    This is it:

    Code:
    LRESULT CALLBACK MainWndProc(
        HWND hwnd,        // handle to window
        UINT uMsg,        // message identifier
        WPARAM wParam,    // first message parameter
        LPARAM lParam)    // second message parameter
    { 
     
        switch (uMsg) 
        { 
            case WM_CREATE: 
                // Initialize the window. 
                return 0; 
     
            case WM_PAINT: 
                // Paint the window's client area. 
                return 0; 
     
            case WM_SIZE: 
                // Set the size and position of the window. 
                return 0; 
     
            case WM_DESTROY: 
                // Clean up window-specific data objects. 
                return 0; 
     
            // 
            // Process other messages. 
            // 
     
            default: 
                return DefWindowProc(hwnd, uMsg, wParam, lParam); 
        } 
        return 0; 
    }
    So in Microsoft's example, they only call DefWindowProc if the message has not been processed by the Window Procedure. Is this incorrect? Should that "return 0" read "return DefWindowProc(hwnd, uMsg, wParam, lParam)"?

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This is correct. Return at the end maybe never gets executed but this is the correct window message handler. Why should you doubt in MSDN examples anyway?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Quote Originally Posted by maxorator
    Why should you doubt in MSDN examples anyway?
    Let me give you an example of why. Take the example code at the bottom of this link: http://msdn.microsoft.com/library/de...ialogboxes.asp

    I'm talking about this function specifically:
    Code:
    LPWORD lpwAlign(LPWORD lpIn)
    {
        ULONG ul;
    
        ul = (ULONG)lpIn;
        ul ++;
        ul >>=1;
        ul <<=1;
        return (LPWORD)ul;
    }
    The structures need to be DWORD-aligned in memory. Not WORD-aligned. The calls to this function contain comments that even mention that it needs to be aligned on a DWORD boundary. So why does their alignment function work on WORD boundaries instead? It happens to work with their example program, but try, for instance, to change their "My Dialog" title to something containing an even number of characters.

    Just above the code they even have this (which is flat out wrong):
    this example uses a helper routine that takes an input pointer and returns the closest pointer that is aligned on a DWORD boundary.

    It's always good to question things. Don't take anything for granted. Especially if it comes from Microsoft.
    Last edited by itsme86; 09-18-2006 at 10:49 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by maxorator
    Why should you doubt in MSDN examples anyway?
    Because you just told me:

    Quote Originally Posted by maxorator
    DefWindowProc should be called in addition to your message handling
    but the example I just pasted in doesn't do that, for the reasons I outlined!

    Quote Originally Posted by kidburla
    So in Microsoft's example, they only call DefWindowProc if the message has not been processed by the Window Procedure. Is this incorrect?
    Quote Originally Posted by maxorator
    This is correct.
    Which is it?

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    The ladder.

    However, when you return your proc with 0, I think there is some kind of automatic "refresh" call to the def proc.
    Last edited by Queatrix; 09-18-2006 at 12:27 PM.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by kidburla
    What does DefWindowProc actually do? I mean, is it just a function which returns 0 for WM_QUIT and 1 for all other messages, or does it actually do some processing?
    Whatever it needs to do for a particular message. For example, if you allow D to answer the call for WM_MOVE and WM_SIZE it will set all the window position vars for you.
    Quote Originally Posted by kidburla
    Is there somewhere where we could find out what DefWindowProc does for each individual message?
    I have not found a location on the MSDN that gives what D does for EACH message, but then again, I haven't found a location that lists EVERY message either. . . I do know, however, that there is quite a bit of information spread throughout the MSDN about DefWindowProc. Mostly, if my memory serves me correctly, I think it is where each message is discussed.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> I haven't found a location that lists EVERY message either.

    Try your compiler includes, right under you nose.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by itsme86
    The structures need to be DWORD-aligned in memory.
    Actually, I found the control structures needed to be WORD aligned for memory dialogs. But I agree entirely with your general point; there's some msdn sample code and documentation that's very wrong and your advice on taking nothing for granted is most prudent.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I haven't found a location that lists EVERY message either.
    winuser.h? Maybe search MSDN or something...

    Double-checking a message is not needed, if you don't handle messages you are not meant to handle.

    Yes, MSDN often give some compiler-specific code (THEIR compiler specific) and some things that are not quite following the standards (like void main).
    Last edited by maxorator; 09-18-2006 at 01:42 PM.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by Kennedy
    I do know, however, that there is quite a bit of information spread throughout the MSDN about DefWindowProc. Mostly, if my memory serves me correctly, I think it is where each message is discussed.
    Thanks very much. I looked there, and found all the info I need.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There are two common WndProc structures.

    1) This one is used most often in sample code.
    Code:
    int CALLBACK WndProc(...)
    {
      switch(msg)
      {
      case WM_xxx:
        break;
    
      case WM_yyy:
        break;
    
      default:
        return DefWindowProc(...);
      }
      return 0;
    }
    2) This is the one I prefer, because it allows you to choose whether to call DefWindowProc or not for each message, so you can do additional processing as well as instead-of processing of messages.
    Code:
    int CALLBACK WndProc(...)
    {
      switch(msg)
      {
        case WM_xxx:
          return 0; // Don't call DefWindowProc.
    
        case WM_yyy:
          break; // Do call DefWindowProc.
      }
      return DefWindowProc(...);
    }
    There are some rare cases where this is useful.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this example work for me?
    By xixpsychoxix in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 08:25 PM
  2. custom control from scratch
    By underthesun in forum Windows Programming
    Replies: 8
    Last Post: 01-19-2005, 12:29 AM
  3. WM_PAINT through DefWindowProc
    By Benzakhar in forum Windows Programming
    Replies: 4
    Last Post: 03-06-2004, 07:17 PM
  4. Window closes when edit is made
    By eam in forum Windows Programming
    Replies: 7
    Last Post: 11-06-2003, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21