MFC image with the news of dynamic menu

Author:Anonymous    Updated:2008-3-20 23:13:52
When we talk about the realization of dynamic menu, we usually approach is the use of GetMenu () function Cmenu access to a pointer type, and then call CMenu class Method AppendMenu, InsertMenu, ModifyMenu, such as RemoveMenu. This paper introduces a more concise way, it uses the MFC source image CCmdUI type of the methods and mechanisms to achieve.

First, we briefly talk about the news in VC MFC image. Windows programmers each of the past probably use the window function WindowProc remember, when we are faced with all kinds of information, we have no other parties can only use the massive and mechanical switch-case statement to achieve different branches of choice. VC5.0 in the use of V4.2 version of the basic MFC class library, you can bid farewell to switch-case statement, replacing it with a transparent image information. In a class to use news images in the category statement, the accession must be explicit Acer DECLARE_MESSAGE_MAP:

Class CMyClass: public CBaseClass
(
DECLARE_MESSAGE_MAP ()
)

In the category to achieve, we must use the two-BEGIN_MESSAGE_MAP and END_MESSAGE_MAP, BEGIN_MESSAGE_MAP with two parameters: the current class and direct Father categories:

BEGIN_MESSAGE_MAP (CMyClass, CBaseClass)
/ / Image of the news

ON_COMMAND (ID_APP_ABOUT, OnAppAbout)

/ / Image of the news
END_MESSAGE_MAP ()

Use the following news image of the basic syntax:

ON_MessageName (ID, ClassMethod)

MessageName news is the need to address, ID information is sent identifier, and ClassMethod to deal with the news of the method name. MessageName MFC is scheduled to justice can be divided into the following three:

Order information
Sub-window alerts
Windows Messages

A total of more than 100, users do not have to remember them, because the source image can be very simple use ClassWizard join. Dealing with a method ClassMethod news category in the class definition must be stated and there is realization of the code. Its prototype:

Afx_msg return_type ClassMethod (paras table)

Class CCmdUI specialized (and only) and ON_UPDATE_COMMAND_UI news image-matching use for the management of the menu (and the toolbar buttons, etc.) real-time status, such as whether Bianhui whether Canada selected markings.

Acer prototype image ON_UPDATE_COMMAND_UI news:

ON_UPDATE_COMMAND_UI (Menu_Item_ID, Menu_Proc)

ON_UPDATE_COMMAND_UI-news image of a menu item (orders), and a link to update process, which automatically at the appropriate time calling this update process to complete the menu item status updates.

Menu_Item_ID menu items for the ID, Menu_Proc update this menu item processing function, as follows:

Afx_msg void Menu_Proc (CCmdUI pCmdUI *)

CCmdUI it with a pointer type, it can be used like calling CCmdUI methods. And the menu of ways:

Enable (BOOL) to menu items valid or invalid
SetText (LPCTSTR) settings menu item text
SetCheck (int) or remove the selected marker "X"
SetRadio (BOOL), plus selected marker or removed "."

MenuProc be called the timing of the following circumstances:

With your mouse, select the menu item contains the menu of
Using hotkey contains the selected menu item of the menu
Shortcuts using selected with the same menu in the menu items under the menu item as a

We Below menu structure as an example:

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 using the left mouse button click menu of the Test menu or by pressing Alt + t or press Ctrl +1/2/3 / 4, the four menu items MenuProc update process will be called.
When we inspected the above menu with nested structure, we are facing such a problem: Menu Item Item One / Item Two of the update function and Popup One / Popup Two of the update function consistent form? When Popup One and Popup Two are Bianhui when Popup is automatically Bianhui?

According to MFC's internal mechanisms, only the first pop-up menu should be some additional code and the rest of the form is basically the same. That is in the case of, in addition to menu items Popup One, the other menu items updated code essentially the same function, that is, under conditions of simple methods can be called CCmdUI category. Popup One menu item because of the pop-up menu Popup is the first of its update function in the following two situations will be called:

When the pop-up menu (Popup) menu items (Popup One and Popup Two) to be charted,

When the pop-up menu that Popup itself to be drawn,

The first is well understood, as we Test menu and select Item One Item Two and the function will automatically update the implementation of the same. The second situation is also very natural, because Popup and Item One / Item Two not the same, it does not have ID, can add image of the news, then the state how to update it? It was the first update function to be called, in order to distinguish between different call, it will CCmdUI members of the class variables m_pSubMenu set to a different value. In the first case, m_pSubMenu equivalent NULL, the second case, m_pSubMenu does not mean NULL.

Here we are given a practical programming examples. Because of space, we are only some of the key statements, while the rest are omitted together.

In the first statement of categories of documents:

BOOL m_bItemOne, m_bItemTwo, m_bPopupOne, m_bPopupTwo;
/ / Used to determine the status of various menu items
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);
/ / The update function of the menu items
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_bItemTwo) 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_ENABLED:
(MF_DISABLED | MF_GRAYED)));
Return;
)
PCmdUI-> Enable (m_bPopupOne);
)

Void CMyApp:: OnUpdatePopupTwo (CCmdUI pCmdUI *)
(
PCmdUI-> Enable (m_bPopupTwo);
)
Previous:Use memory-mapped file to improve the performance you
Next:MFC message handling procedures order
User Reviews
Related Articles
Recommended article
AD