When we refer to the implementation of a dynamic menu, our usual practice is to use the GetMenu () function to get a CMenu class pointer, and then invoke the CMenu class method AppendMenu, InsertMenu, Modifymenu, Removemenu, and so on. This paper introduces a more concise method, which utilizes the message image mechanism of MFC and the CCmdUI class method to implement it.
First, we briefly talk about the MFC message image in VC. Every Windows programmer probably remembers the window functions previously used WindowProc, when we face various messages, we have no other side, we can only use a large and mechanical switch-case statements to achieve different branch choice. Using the V4.2 version of the MFC base Class library in VC5.0, you will say goodbye to the Switch-case statement and replace it with a transparent message image. To use a message image in a class, you must explicitly join the macro Declare_message_map in the class declaration:
class CMyClass: public CBaseClass
{
DECLARE_MESSAGE_MAP()
}
In a class implementation, you must use two macros Begin_message_map and end_message_map,begin_message_map with two parameters: the current class and the immediate parent class:
BEGIN_MESSAGE_MAP(CMyClass, CBaseClass)
// 消息映像项
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// 消息映像项
END_MESSAGE_MAP()
Message image entries Use the following basic syntax:
ON_MessageName(ID, ClassMethod)
MessageName is the message that needs to be processed, the ID is the identifier that sent the message, and Classmethod is the class method name that handles the message. MessageName is predefined by MFC and can be divided into the following three kinds:
• Command message
• child window notification message
· Windows messages
A total of more than 100 users do not have to remember them, because the message image can be easily used ClassWizard join. The class method that handles a message Classmethod must be declared in the class definition and has implementation code. Its prototype is: