Reputation: 433
I have a menu bar across the top of my dialog and one of the options is "mode" which contains "normal" and "debug". I'm trying to make it so that when the user clicks either of these two options, a checkmark will appear next to the last selected item in the dropdown menu.
This is what I found from searching around google, but I can't get it to work:
//event handler for user clicking on mode then normal in the menu
void CNew_RGB_ControlDlg::OnModeNormal()
{
//check the normal option when the user selects normal mode in the menu
CMenu menu;
menu.LoadMenu(IDR_MENU1);
menu.CheckMenuItem(ID_MODE_NORMAL, MF_CHECKED | MF_BYCOMMAND); //returns 8
menu.CheckMenuItem(ID_MODE_DEBUG, MF_UNCHECKED | MF_BYCOMMAND);//returns 0
}
I also have another of these functions for when debug is clicked, its the same code just the checked and unchecked are switched.
The return values make it seem like it should be working according to MSDN, but the menu items never change.
I have also tried this:
void CNew_RGB_ControlDlg::OnModeNormal()
{
CMenu menu;
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(1)->CheckMenuItem(0, MF_BYPOSITION|MF_CHECKED);
menu.GetSubMenu(1)->CheckMenuItem(1, MF_BYPOSITION|MF_CHECKED);
}
What am I doing wrong? What do I need to do to make this work?
Upvotes: 2
Views: 7365
Reputation: 1
Any way to check/uncheck a menu item on the main menu bar?
My old code (from View Class)
CWnd* pParent = GetParent();
CMenu* pMenu = pParent->GetMenu();
pMenu->CheckMenuItem(ID_TEST_1, MF_UNCHECKED);
pMenu->CheckMenuItem(ID_TEST_2, MF_UNCHECKED);
No longer works in VC++ 2010. They say it has something to do with floating toolbars and/or active accessibility.
Nat Hager
Upvotes: 0
Reputation: 328
Declare your CMenu item as a member of the dialog class so it persists after you load the resource the first time, by declaring it in the handler and loading it you are operating on a different copy of the menu each time. You could also load it dynamically each time using ::GetMenu() as another response shows.
When you create the menu before displaying it you have to check the menu item you want and uncheck the others something like this:
// in CNew_RGB_ControlDlg.h
CMenu menu;
// in CNew_RGB_ControlDlg::OnInitDialog
// no need to get submenu if you use the menu id
menu.LoadMenu(IDR_MENU1);
menu.CheckMenuItem(ID_NORMAL, MF_CHECKED);
menu.CheckMenuItem(ID_DEBUG, MF_UNCHECKED);
}
When you respond to the command that set a different menu item as checked
switch(Command)
{
case ID_NORMAL:
menu.CheckMenuItem(ID_NORMAL, MF_CHECKED);
menu.CheckMenuItem(ID_DEBUG, MF_UNCHECKED);
break;
case ID_DEBUG:
menu.CheckMenuItem(ID_NORMAL, MF_UNCHECKED);
menu.CheckMenuItem(ID_DEBUG, MF_CHECKED);
break;
};
Upvotes: 0
Reputation: 5132
Instead of loading a fresh menu when an item is selected, you would need to get the current menu used in the dialog, like
CMenu *pMenu = GetMenu();
if (pMenu != NULL)
{ pMenu->CheckMenuItem(ID_MODE_NORMAL, MF_CHECKED | MF_BYCOMMAND);
pMenu->CheckMenuItem(ID_MODE_DEBUG, MF_UNCHECKED | MF_BYCOMMAND);
}
Upvotes: 3