Analysis of MFC Message Response Mechanism

zhaozj2021-02-11  165

MFC Message Response Mechanism Analysis Zhejiang University Computer Department Hu Zhaohui Chen Qi Yu Rui

---- Abstract: ---- MFC is the most popular type library of Windows down designs, but this class is relatively uncommon, especially its message mapping mechanism, which involves a lot of low-level things, we are Here, the entire message mapping mechanism has been analyzed, which can help program developers have a relatively thorough understanding of the MFC's message mapping mechanism. ---- Keywords: object-oriented message mapping MFC program design one. Introduction ---- VC MFC class library is actually the most popular class library for C programming under Windows. The frame structure of the MFC is greatly facilitated by programmers, but in order to more effective, flexible use of MFC programming, understanding the MFC architecture often makes programming work more than half. It encloses the Win32 API function and designs a convenient message mapping mechanism. But this mechanism itself is large and complicated, and it is undoubtedly to help us write more reasonable and efficient procedures. Here we simply analyze the Message response mechanism of the MFC to understand how the MFC is packaged for Windows, which is convenient for users to develop. two. Message Mechanism under SDK ---- Here's simple review of how we do Windows under SDK. In general, Windows's messages are corresponding to the thread. That is, Windows will send messages to threads corresponding to the message. In the SDK mode, the program takes the message from the message queue corresponding to a getMessage function and puts it in a special structure, and a message is a Structure. Typedef struct tagmsg {

Hwnd hwnd;

Uint Message;

WPARAM WPARAM;

LParam Lparam;

DWORD TIME;

Point Pt;

} Msg;

---- In which hwnd represents the handle of the window, Message indicates the ID number of the message, the WPARAM, and LPARAM representation, and the message-related parameters, and the time indicates the time of the message transmission, the PT represents the position of the mouse when the message is sent. ---- Then the TranslateMessage function is used to translate the virtual key message into a character message and put it in a response message queue, and finally the DispatchMessage function distributes the message to the related window process. The window will then process different messages depending on the type of message. During the SDK programming process, the user needs to analyze the type of message during the window and the meaning of the parameters together with the message, do different processing, relatively trouble, and the MFC will be encapsulated by the message call, and the user can pass ClassWizard. Easy to use and process various messages of Windows. three. MFC message implementation mechanism - we can see that under the frame structure of the MFC, the header file of the message processing can contain the declare_message_map () macro, which is mainly made by the statement of message mapping and message processing functions. The following configuration can be generally included in the implementation file of the class processing class. Begin_MESSAGE_MAP (Cinheritclass, CBaseClass)

// {{AFX_MSG_MAP (Cinheritclass)

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

---- Here mainly performs the implementation of message mapping and implementation of message processing functions. ---- All classes that can perform message processing are based on CCMDTARGET classes, that is, the CCMDTARGET class is all parent classes that can perform messages. The CCMDTARGET class is the basis and core of the MFC handling command message. ---- At the same time, MFC defines the following two main structures: AFX_MSGMAP_ENTRY

STRUCT AFX_MSGMAP_ENTRY

{

Uint NMessage; // Windows Message

Uint ncode; // control code or wm_notify code

UINT NID;

// Control ID (or 0 for Windows Messages)

Uint nlastid;

// use for entries Specifying a Range of Control ID's

Uint nsig;

// Signature Type (action) or Pointer to Message #

AFX_PMSG PFN; // Routine to Call (Or Special Value)

}

And AFX_MSGMAP

Struct AFX_MSGMAP

{

#ifdef _AFXDLL

Const AFX_MSGMAP * (Pascal * PfNgetBaseMap) ();

#ELSE

Const AFX_MSGMAP * PBASEMAP;

#ENDIF

Const AFX_MSGMAP_ENTRY * LPENTRIES;

}

Where the AFX_MSGMAP_ENTRY structure contains

All related information for a message, where

NMessage ID number for Windows Message

Ncode is a notification code for controlling messages

NID ID for Windows Control Messages

NLASTID means that if it is a specified range of messages,

NLASTID is used to represent its range.

NSIG indicates the action identification of the message

AFX_PMSG PFN It is actually a pointing

And the pointer to the message corresponding to the message.

---- AFX_MSGMAP main role is two, one: the message map of the base class is used to get the base class. Two: Get the message map of the message. ---- In fact, MFC fills all messages into the AFX_MSGMAP_ENTRY structure, forming an array, the array stores all messages and related parameters associated with them. At the same time, the first address of the array can be obtained by AFX_MSGMap, and the message map of the base class is obtained, which is the message response of the base class when it is not responding to the message. ---- Now let's analyze how the MFC will make the window process to process messages. In fact, all MFC window classes intercept messages through hook functions_afxcbtfilterhook, and set the window process to AFXWndProc in the hook function _afxcbtfilterhook. The original window process is saved in the member variable m_pfnsuper. ---- So under the MFC framework, the processing of a message is usually like this.

Function AfxWndProc Receives the message sent by the Windows operating system. Function AFXWndProc call function AFXCallWndProc performs message processing, here a progress is to convert the operation of the handle into the operation of the CWND object. Function AFXCallWndProc calls the CWND class method WindowProc for message processing. Note that AfxWndProc and AFXCallWndProc are AFX's API functions. WindowProc is already a way to CWnd. So you can notice that there is no parameter for the handle or CWnd in WindowProc. Method The WINDOWPROC call method OnWndMSG performs formal message processing, that is, the message delivery is sent to the relevant method. How is the message delivered? In fact, an AFX_MSGMAP structure is saved in the CWnd class, and the portal of the array of messages generated by ClassWizard is saved in the AFX_MSGMap structure, and we compare all the Message sent to OnWndMSG and all the Message in the array. Find the one message that matches. In fact, the system is implemented by function AFXFindMessageEntry. Found that message, in fact, we got an AFX_MSGMAP_ENTRY structure, and we have mentioned all the information related to the message, where the main is the execution function of the message, and the message-related execution function. Then we can invoke related execution functions according to the action of the message, and this execution function is actually a method defined in class implementation through ClassWizard. This transforms the processing of the message to a method in the class. For a simple example, for example, the processing of the WM_LBUTTONDOWN message is transformed into actions of the following method in the process of the VIEW. Void cinheritview :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT)

{

// Todo: add your message

Handler code Here and / or call default

CView :: ONLBUTTONDOWN (NFLAGS, POINT);

}

转载请注明原文地址:https://www.9cbs.com/read-5733.html

New Post(0)