MFC messaging image image real dynamic menu

zhaozj2021-02-11  221

---- When we mentioned the real current menu, our common way is to use the getMenu () function to take a cMenu class pointer, then call CMenu class method appendmenu, insertmenu, modifymenu, removemenu, etc. This article introduces a more simple method, which uses MFC's message image mechanism and CCMDUI class method.

---- First, I simply say that the MFC's message image in the VC. Each Windows programmer is overwhelming the window function of the window, WINDOWPROC, and when we face a variety of messages, I don't have any other side, and can only use a large and mechanical Switch-Case sentence to realize different branch options. In VC5.0, use the V4.2 version of the MFC basic class library, you will notonize the Switch-Case statement, and the generation is in the transparent message. To use a message image in a class, in the class sound, you must explicitly join the macro declare_MESSAGE_MAP:

Class CMYCLASS: Public CBaseClass

{

Declare_message_map ()

}

---- In-class implementation, must use two macro begin_MAP_map and end_message_map, begin_message_map with two parameters: When the prior class and direct parent classes:

--- Begin_Message_Map (CMYCLASS, CBASECLASS) ---- // Message Image

---- ON_COMMAND (ID_APP_ABOUT, ONAPPABOUT)

---- // Message Image ---- END_MESSAGE_MAP ()

---- Message image item Use the following basic statement: ---- ON_MESSAGENAME (ID, ClassMethod)

---- MessageName is a message that needs to be processed, the ID is the identifier of sending a message, and ClassMethod is a classmate name of the work of this message. MessageName is predefined by MFC, which can be divided into three types:

---- Command message ---- Sub-window Notch Member ---- Windows Message

---- There are more than one hundred, users don't have to remember them, because the message image can be added with ClassWizard with a simple use. The method of processing a message ClassMethod must be in the class definition, and there is a real current code. Its prototype is:

---- AFX_MSG RETURN_TYPE CLASSMETHOD (Paras Table)

---- Class CCMDUI special door (only) and on_update_command_UI message image macro supporting, used for real-time state states for administrative menu (also in toolbar, etc.), if it is a variable gray, whether or not to select the markup.

---- ON_UPDATE_COMMAND_UI Message Image Macro Protected: ---- ON_UPDATE_COMMAND_UI (MENU_ITEM_ID, MENU_PROC)

---- ON_UPDATE_COMMAND_UI Message Image Macro will join a menu item (command) and a newer handling process, from the appropriate timing automatic calling this update process to the menu item status.

---- MENU_ITEM_ID is the ID number of the menu item, MENU_PROC's update function for this menu item, prototype:

---- AFX_MSG Void Menu_Proc (ccmdui * PCMDUI)

---- It has a CCMDUI pointer that uses it to call CCMDUI's class. Camel method with the menu:

Enable (BOOL) makes the menu item valid or invalid

SetText (LPCTSTR) Sets the text of the menu item

SetCheck (int) plus or removes the "X"

Setradio (BOOL) plus or removes the tag "."

---- MenuProc is called the next few cases:

---- Use the mouse to include the menu bar of the menu item ---- Use the hotkey to select the menu bar containing the menu item ---- use shortcuts to select any menu under the same menu item with the menu item item

---- We are clarified as the following menu:

Test Menu

Item One ID_Item_one Ctrl 1

Item TWO ID_Item_Two Ctrl 2

Popup popup one id_popup_one ctrl 3

Popup TWO ID_POPUP_TWO CTRL 4

---- When you use a mouse left button to press the Test Menu menu or press Alt T or press Ctrl 1/2/3/4, the updated handling process of the four menu items will be called.

---- When we examine the menu with nested structured menu, our question: Does menu item Item One / Item TWO update function and Popup One / Popup TWO update function? When popup one and popup two become gray, Popup does not motivate ash?

---- According to the internal machine of the MFC, only the first item of the population should be attached to the menu, and the residue of the remaining form is consistent. That is to say, in the above example, in addition to the menu item popup one, the code base of his menu item update function is based, that is, according to the condition, simply call the CCMDUI class method. The menu item Popup One is made by the first item of the poppet Popup, and its update function is called in the following two cases:

---- When the menu item (POPUP) menu item (Popup One and Popup TWO) are to be drawn ---- When this poppet, Popup is to be plotted

---- The first situation is very good, just as I choose Test Menu and Item One and Item TWO updates will be automatically executed. The second situation is also very natural, because it is not the Popup and Item One / Item TWO, it does not have the ID number, can not add a message image, what is its status? The update function of its first item is called, which is different from the distinction, which sets the CCMDUI's class member variable M_PSUBMENU to different values. In the first circumstance, m_psubmenu is equal to NULL, in the second case, M_PSUBMENU does not equal NULL.

---- I have given an actual programming model. By the artificial relationship, I only give only some key statements, and the remaining will be omitted.

---- In the class sound in the header:

BOOL M_BITEMONE, M_BITEMTWO, M_BPOPUPONE, M_BPOPUPTWO;

// Used to determine the state of each menu item

protected:

AFX_MSG Void OnupdateMenuItemone (CCMDUI * PCMDUI);

AFX_MSG Void OnupdateMenuiteMtwo (CCMDUI * PCMDUI);

AFX_MSG Void OnupdatePopupone (CCMDUI * PCMDUI);

AFX_MSG Void onupdatePopuptwo (CCMDUI * PCMDUI);

// Update function of each menu item

Declare_message_map ()

In the source file:

Begin_MESSAGE_MAP (CMYDOC, CDocument)

ON_UPDATE_COMMAND_UI (id_item_one,

OnUpdateMenuItemone)

ON_UPDATE_COMMAND_UI (id_item_two,

OnupdateMenuItemTwo)

ON_UPDATE_COMMAND_UI (ID_POPUP_ONE,

OnupDatePopupone)

ON_UPDATE_COMMAND_UI (ID_ popup_two,

OnUpdatePopuptwo)

END_MESSAGE_MAP ()

Void CMYApp :: OnupdatetMenuItemone (ccmdui * pcmdui)

{

PCMDUI-> Enable (M_BITEMONE);

IF (m_bitemone) PCMDUI-> SetText ("Item One");

Else Pcmdui-> SetText ("Item One Is Now Disabled";

}

Void CMYAPP :: OnUpdatetMenuiteMtwo (ccmdui * pcmdui)

{

PCMDUI-> Enable; M_BiteMtwo;

IF (m_bitwo) PCMDUI-> SetText ("Item TWO");

Else Pcmdui-> SetText ("item two is now disabled");}

Void CMYAPP :: OnUpdatePopupone (ccmdui * pcmdui)

{

IF (PCMDUI-> m_psubmenu! = null)

{

BOOL B_POPUP = M_BPOPUPONE || M_BPopuptwo;

PCMDUI-> m_pMenu-> EnableMenuItem (pcmdui-> m_nindex,

MF_BYPOSITION |

(Benable? MF_ENABED:

(MF_DISABED | MF_GRAYED)))))))))

Return;

}

PCMDUI-> Enable; M_BPopupUpone

}

Void CMYAPP :: OnUpdatePopuptwo (ccmdui * pcmdui)

{

PCMDUI-> Enable (M_BPopuptwo);

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

New Post(0)