description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CMFCMenuButton Class |
CMFCMenuButton Class |
07/15/2019 |
|
|
53d3d459-1e5a-47c5-8b7f-2e61f6af5187 |
A button that displays a pop-up menu and reports on the user's menu selections.
class CMFCMenuButton : public CMFCButton
Name | Description |
---|---|
CMFCMenuButton::CMFCMenuButton | Constructs a CMFCMenuButton object. |
Name | Description |
---|---|
CMFCMenuButton::PreTranslateMessage | Called by the framework to translate window messages before they are dispatched. (Overrides CMFCButton::PreTranslateMessage .) |
CMFCMenuButton::SizeToContent | Changes the size of the button according to its text and image size. |
Name | Description |
---|---|
CMFCMenuButton::m_bOSMenu | Specifies whether to display the default system pop-up menu or to use CContextMenuManager::TrackPopupMenu. |
CMFCMenuButton::m_bRightArrow | Specifies whether the pop-up menu will appear underneath or to the right of the button. |
CMFCMenuButton::m_bStayPressed | Specifies whether the menu button changes its state after the user releases the button. |
CMFCMenuButton::m_hMenu | A handle to the attached Windows menu. |
CMFCMenuButton::m_nMenuResult | An identifier that indicates which item the user selected from the pop-up menu. |
CMFCMenuButton::m_bDefaultClick | Allow default (on button text/image) processing. |
The CMFCMenuButton
class is derived from the CMFCButton Class which is, in turn, derived from the CButton Class. Therefore, you can use CMFCMenuButton
in your code the same way you would use CButton
.
When you create a CMFCMenuButton
, you must pass in a handle to the associated pop-up menu. Next, call the function CMFCMenuButton::SizeToContent
. CMFCMenuButton::SizeToContent
checks that the button size is sufficient to include an arrow that points to the location where the pop-up window will appear - namely, underneath or to the right of the button.
The following example demonstrates how to set the handle of the menu attached to the button, resize the button according to its text and image size, and set the pop-up menu that is displayed by the framework. This code snippet is part of the New Controls sample.
[!code-cppNVC_MFC_NewControls#38] [!code-cppNVC_MFC_NewControls#39]
Header: afxmenubutton.h
Constructs a new CMFCMenuButton object.
CMFCMenuButton();
A Boolean member variable that indicates which pop-up menu the framework displays.
BOOL m_bOSMenu;
If m_bOSMenu
is TRUE, the framework calls the inherited TrackPopupMenu
method for this object. Otherwise, the framework calls CContextMenuManager::TrackPopupMenu.
A Boolean member variable that indicates the location of the pop-up menu.
BOOL m_bRightArrow;
When the user presses the menu button, the application shows a pop-up menu. The framework will display the pop-up menu either under the button or to the right of the button. The button also has a small arrow that indicates where the pop-up menu will appear. If m_bRightArrow
is TRUE, the framework displays the pop-up menu to the right of the button. Otherwise, it displays the pop-up menu under the button.
A Boolean member variable that indicates whether the menu button appears pressed while the user makes a selection from the pop-up menu.
BOOL m_bStayPressed;
If the m_bStayPressed
member is FALSE, the menu button does not become pressed when the uses clicks the button. In this case, the framework displays only the pop-up menu.
If the m_bStayPressed
member is TRUE, the menu button becomes pressed when the user clicks the button. It stays pressed until after the user closes the pop-up menu, either by making a selection or canceling.
The handle to the attached menu.
HMENU m_hMenu;
The framework displays the menu indicated by this member variable when the user clicks the menu button.
An integer that indicates which item the user selects from the pop-up menu.
int m_nMenuResult;
The value of this member variable is zero if the user cancels the menu without making a selection or if an error occurs.
Allows default processing of text or images on the button.
BOOL m_bDefaultClick;
Setting m_bDefaultClick to false causes the button to show the menu when you click anywhere on the button.
Called by the framework to translate window messages before they are dispatched.
virtual BOOL PreTranslateMessage(MSG* pMsg);
pMsg
[in] Points to a MSG structure that contains the message to process.
Nonzero if the message was translated and should not be dispatched; 0 if the message was not translated and should be dispatched.
Changes the size of the button according to its text size and image size.
virtual CSize SizeToContent(BOOL bCalcOnly = FALSE);
bCalcOnly
[in] A Boolean parameter that indicates whether this method resizes the button .
A CSize object that specifies the new size for the button.
If you call this function and bCalcOnly is TRUE, SizeToContent
will calculate only the new size of the button.
The new size of the button is calculated to fit the button text, image, and arrow. The framework also adds in predefined margins of 10 pixels for the horizontal edge and 5 pixels for the vertical edge.