System understanding Win32 API and MFC 2

Source: Internet
Author: User

Ii. concept model of MFC

We have studied the "Domain Model" of WIN32 API and have a comprehensive understanding of it. Next, we will focus on the research of the MFC conceptual model on the app framework.
The message response/transfer mechanism in the app framework is the most important. The Hook mechanism is closely related to the Message response/transmission mechanism, which is based on the former.

1. Hook Mechanism

Some programmers may only know that the hook mechanism can be used to write very "awesome" applications. I don't know that MFC also relies on the hook mechanism.

Each hook has a pointer queue, and each Pointer Points to a HookProc function. HookProc will be called and executed by the OS at the right time. Hook is minute
Different types of hooks determine when the hook is called and executed by the OS. TIPS: You can take a look at the "subscription-release" design mode to help you understand.

2 install the Message Response Function in MFC

2.1 recall the installation of the Message Response Function in the API

The installation of the Message Response Function in the API is implemented by CreateWindow (). It associates the window with a windowClass, and the latter records the pointer of the Message response function.
For details, it is clear how to use Win32 SDK or Win16 SDK to write a program. DefWindowProc () is an API function that provides default message processing, programmers only need handle messages that require special processing.

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
WNDCLASS wndclass;
...
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName = szWindowClass;
...
RegisterClass(&wndclass);
hWnd = CreateWindow( szWindowClass, ...);
...
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
...
return;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

2.2 install the Message Response Function in MFC

In MFC, the installation of the Message response function is obviously more complex. It is completed when CWnd: CreateEx () is called, and the Hook mechanism is also used.

We can first guess how MFC works. MFC supports massage map, so that the response to messages is dispersed into multiple message handler functions, rather than the centralized message processing function developed by API; therefore, there must be special code for "Retrieving messages"
Map table and then call message handle ". Message map is used to support programmers to process special messages of interest to them. Where is the default message processing logic? The answer is that MFC creates a window.
Obj uses the "pre-defined window class", which naturally has the default message processing function.

As you can see, CWnd has member variables m_pfnSuper, member variables m_hWnd, member functions OnWndMsg () and member functions DefWindowProc (). Wnd: OnWndMsg () is responsible
The message handle defined in map can process the incoming message. If yes, true is returned. CWnd: DefWindowProc () is responsible for the default processing of the message.
The execution process is: first, CWnd: CreateEx () is called, and window obj and window class are created accordingly. At this time, window
The WindowProc field of the class stores the address of the predefined default handler function. Because a hook creates a message in the listener window, the registered hookProc () will be called.
Line, which backs up the WindowProc field of the classWindow data structure to CWnd: m_pfnSuper, and then rewrite it with SetWindowLong ()
The WindowProc field of the classWindow data structure is: AfxWndProc () address. When any message arrives
: AfxWndProc () is called. As for its logic, you must have guessed it. Call Wnd: OnWndMsg () first. If the returned value is false, call
CWnd: DefWindowProc (), CWnd: m_pfnSuper points to the default processing logic, also in CWnd: DefWindowProc ()
.
Note: The above is actually caused by polymorphism. For example, you can search pWnd-> WindowProc (nMsg, wParam, lParam). In addition, OnWndMsg and DefWindowProc are both CWnd-class virtual functions.

If you do not think it is easy to understand, it is best to create a project in VC ++ to track it. below is what I call the stack image during tracking.

3. SubClass Mechanism


We can see that the SubClass mechanism is based on the m_pfnSuper of CWnd, and is very similar to the "Install Message Response Function in MFC.

4. Main related classes in frame work


The main related class in frame work is the candidate for message route. It is their on1_msg () that completes the message route together, forming the chain
Of responsability mode.

5. chain of responsability mode in frame work

Is an object tree. Note that messages are transmitted in both vertical and horizontal directions.


Messages are transmitted in the vertical direction in the massge map table of the parent class. The message map of MFC is completely used to replace the virtual function.
Route is irrelevant.

Message route is the message transmission in the horizontal direction, and the chain of responsability mode is implemented by on1_msg () of multiple related classes.


III,
Summary


From the above discussion, it is not difficult to find that many design modes are used in MFC, such as the chain of responsability mode, composite mode, and subscription-release mode mentioned above. The above discussion not only helps programmers fully master Win32
API and MFC are also very helpful for the architect design architecture.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.