Home > Software engineering >  CMFCMenuButton menu how to access a menu item's check state
CMFCMenuButton menu how to access a menu item's check state

Time:09-21

Want to use in the main window CMFCMenuButton control to realize the menu button as shown in the figure below, need to users to click on "allow data collection" menu item when the menu item selected, and change the state of the check, make the Settings to take effect,


The menu resource file for IDR_MENU_STORAGE, "allow data collection" menu item ID for ID_MENU_ALLOW_STORAGE,

My window OnInitDialog () implemented in the following code:
//IDR_MENU_STORAGE
CMenu * pMenuStorage=new CMenu;
If (TRUE==pMenuStorage - & gt; LoadMenu (IDR_MENU_STORAGE))//load the menu resource
{
M_menubtn_storage. M_hMenu=pMenuStorage - & gt; GetSubMenu (0) - & gt; GetSafeHmenu ();
} the else
{
TRACE (" resource load failed!" );
}

In response to a number of lines to the button implementation code is as follows:
Void CATC_TestSvcDlg: : OnBnClickedMfcmenubuttonDatastorage ()
{
//TODO: add the control notification handler code
The switch (m_menubtn_storage m_nMenuResult)
{
Case ID_MENU_ALLOW_STORAGE:
{
CMenu * pMenu=m_menubtn_storage. GetMenu ();
If (pMenu)
{
UINT state=pMenu - & gt; GetMenuState (ID_MENU_ALLOW_STORAGE MF_BYCOMMAND);
ASSERT (state!=0 XFFFFFFFF);
If (state & amp; MF_CHECKED)
PMenu - & gt; CheckMenuItem (ID_MENU_ALLOW_STORAGE, MF_UNCHECKED | MF_BYCOMMAND);
The else
PMenu - & gt; CheckMenuItem (ID_MENU_ALLOW_STORAGE, MF_CHECKED | MF_BYCOMMAND);
}
}
break;
Case ID_MENU_... :
break;
Default:
break;
}

Click "allow data collection" in the execution menu items, after get the response, during program execution to the code red, abnormal as shown:

Display hMenu of unused

This problem for a long time, a great god save

CodePudding user response:

Feeling is the CMenu * pMenu=m_menubtn_storage GetMenu (); The place out of the question, don't know what method can be obtained from CMFCMenuButton object to the corresponding object menu

CodePudding user response:

State of the building is not very good. Check the menu, it is in the void CMainFrame: : OnUpdateXX (CCmdUI * pCmdUI) set (according to the relevant variables), you directly use this judgment related variables,

CodePudding user response:

Reason in your button response code inside the

//TODO: add the control notification handler code
The switch (m_menubtn_storage m_nMenuResult)
{
Case ID_MENU_ALLOW_STORAGE:
{
CMenu * pMenu=m_menubtn_storage. GetMenu ();
If (pMenu)

CMenu * pMenu=m_menubtn_storage. GetMenu (); It is the sentence that is CMenu class no GetMenu () the member function, you write that there is certainly a problem
If you want to get a pointer to the main window you menu then use FromHandle function, its function is to: returns a pointer to the CMenu object of a given Windows menu handle pointer

CodePudding user response:

You do it too complicated, generally do it: directly in you click on the response of the menu item code, it is good to make the check to the corresponding menu item (ticked to have special function), and then make the other menu items disable () is not optional, also has the corresponding function, click on the response of the other each menu item code to do so, I hope you can understand what I mean,

CodePudding user response:

ON_UPDATE_COMMAND_UI ()
PCmdUI - & gt; Check (TRUE/FALSE);

CodePudding user response:

Could you use the menu, the menu does not exist
Using an internal record state variable, a pop-up menu when update the status, time and judgment with internal variables
  • Related