Study MFC message routing / mapping mechanism [rpm]

MFC started many years ago, but has not studied the principles of the internal merely proficient using VC ++ / AppWizard write software only.

As a game programmer, Win32 or do basic technology, so reading some of MFC source code, figuring out the MFC message routing / mapping mechanism.

The following three steps can be described clearly:

1. MFC in each window corresponds to a C ++ class

Windows with the window HWND to mark, C ++ class or a derived class CWnd means. It can be interchangeable between the two, as follows:

CWnd* wnd = CWnd::FromHandlePermanent(hWnd);

//==>

HWND hWnd = wnd-> GetSafeHwnd ();

2. MFC All windows share a window function process AfxWndProc

We know that the first parameter is the window procedure function hWnd, so you can find examples of this parameter according to the CWnd class, and then call the CWnd WindowProc function. This completes the routing message to the window class instance is the window.

3. CWnd :: WindowProc response to the message map for the class

CWnd class or a derived class needs to take in response to the message map using MFC and the functions corresponding to the message ID, the table has BEGIN_MESSAGE_MAP start, end END_MESSAGE_MAP, wherein like ON_COMMOND entry implemented by macros. These macros actually create an array, each entry of the array by its information message ID response function and so on. In WindowProc Lane you can traverse the array to find a corresponding message ID response function.

In fact, the whole process is very simple, I'm just here to talk about a rough process, there are many details of things, such as AfxWndProc is assigned to all the windows of the way through Hook, WindowProc by calling OnWndMsg to process the message map, in search OnWndMsg the buffer pool with an array to speed up the search. If you want to know the contents carefully, then you can read the MFC source code, I am here not long-winded.

Reproduced in: https: //www.cnblogs.com/bigcat/archive/2010/04/21/2004755.html

Guess you like

Origin blog.csdn.net/weixin_33924312/article/details/94006156