c++mfcmessage

MFC Message flow for controls?


In MFC, suppose I have a dialog box, and in this box, it has a child CListCtrl, then I use mouse to click this CListCtrl, we know that eventually an WM_LBUTTONDOWN message is sent to CListCtrl. My question is: How does this WM_LBUTTONDOWN message get there? Two possibilities:

  1. Dialog box first gets this WM_LBUTTONDOWN message , and it finds that mouse click occurs in its child window, then it forwards this message to CListCtrl.
  2. CListCtrl first gets this WM_LBUTTONDOWN message, it can process this message, and if it doesn't care it will forward this message to parent window, i.e. dialog box for further processing.

Which one is true?

Thanks.


Solution

  • Input messages are never sent to a window. They are posted to the message queue associated with a window, waiting to be retrieved through one of the message retrieval functions (GetMessage, PeekMessage, etc.).

    Depending on whether the dialog box is modal or modeless, messages are retrieved by the nested modal loop (for modal dialogs) or the application's message loop. The message is then passed on to DispatchMessage to find the recipient (starting from the topmost visible window under the mouse pointer, which is neither disabled nor transparent), and call into the associated window's window procedure. The window procedure can decide whether it handles the message or not. A window procedure typically calls DefWindowProc to perform default processing if it doesn't handle a message.

    To summarize: The application's message loop (or the nested modal message loop) gets to see the message first and instructs the window manager to deliver the message to the respective recipient.


    A thorough description of Windows message processing is available at About Messages and Message Queues. The description is specific to the Windows API. Since MFC is just a wrapper around the Windows API, the contents apply to MFC as well, even though some of the concepts are hidden in a typical MFC application.