How do I check or change text in a VS2008 menu bar menu item
-
I must be doing something really stupid, but... I cannot get any of the changes to my menu items reflected in visual appearance. If I check a menu item, or change string I can see changes if I test in code, but not when I click on the menu bar. I use standard default bar creation in main frame OnCreate:
CMFCVisualManager::SetDefaultManager(RUNTIME\_CLASS(CMFCVisualManagerWindows)); if (!m\_wndMenuBar.Create(this)) { TRACE0("Failed to create menubar\\n"); return -1; // fail to create } m\_wndMenuBar.SetPaneStyle(m\_wndMenuBar.GetPaneStyle() | CBRS\_SIZE\_DYNAMIC | CBRS\_TOOLTIPS | CBRS\_FLYBY);
I then create a menu message handler in a view and in it I put something like this:
MENUITEMINFO info; TCHAR buf\[32\]; info.cbSize=sizeof(MENUITEMINFO); info.fMask=MIIM\_STATE|MIIM\_ID|MIIM\_STRING; info.fType=MFT\_STRING; info.dwTypeData=0; HMENU hMenusafe= ((CMainFrame\*)(AfxGetApp()->m\_pMainWnd))->m\_wndMenuBar.GetHMenu(); HMENU hMenu=::GetSubMenu(hMenusafe,0); DWORD err=GetLastError(); int er=GetMenuItemInfo(hMenu,3,1,&info); err=GetLastError(); info.cch++; if( info.cch > 32) info.cch = 32; info.dwTypeData=buf; er=GetMenuItemInfo(hMenu,3,1,&info); err=GetLastError(); if(info.fState & MFS\_CHECKED) { info.fState &=~MFS\_CHECKED; info.fState|=MFS\_UNCHECKED; \_tcscpy(buf,\_T("Menu is unchecked")); info.cch=\_tcslen(buf)+1; } else { info.fState &=~MFS\_UNCHECKED; info.fState |=MFS\_CHECKED; \_tcscpy(buf,\_T("Menu is checked")); info.cch=\_tcslen(buf)+1; } er=SetMenuItemInfo(hMenu,3,1,&info); ((CMainFrame\*)(AfxGetApp()->m\_pMainWnd))->DrawMenuBar();
As I click on the menu item I never see a check, nor changed text. However as I am stepping through the code debugging, I see that changes did take place. What am I doing wrong? Any help appreciated Henryk
-
I must be doing something really stupid, but... I cannot get any of the changes to my menu items reflected in visual appearance. If I check a menu item, or change string I can see changes if I test in code, but not when I click on the menu bar. I use standard default bar creation in main frame OnCreate:
CMFCVisualManager::SetDefaultManager(RUNTIME\_CLASS(CMFCVisualManagerWindows)); if (!m\_wndMenuBar.Create(this)) { TRACE0("Failed to create menubar\\n"); return -1; // fail to create } m\_wndMenuBar.SetPaneStyle(m\_wndMenuBar.GetPaneStyle() | CBRS\_SIZE\_DYNAMIC | CBRS\_TOOLTIPS | CBRS\_FLYBY);
I then create a menu message handler in a view and in it I put something like this:
MENUITEMINFO info; TCHAR buf\[32\]; info.cbSize=sizeof(MENUITEMINFO); info.fMask=MIIM\_STATE|MIIM\_ID|MIIM\_STRING; info.fType=MFT\_STRING; info.dwTypeData=0; HMENU hMenusafe= ((CMainFrame\*)(AfxGetApp()->m\_pMainWnd))->m\_wndMenuBar.GetHMenu(); HMENU hMenu=::GetSubMenu(hMenusafe,0); DWORD err=GetLastError(); int er=GetMenuItemInfo(hMenu,3,1,&info); err=GetLastError(); info.cch++; if( info.cch > 32) info.cch = 32; info.dwTypeData=buf; er=GetMenuItemInfo(hMenu,3,1,&info); err=GetLastError(); if(info.fState & MFS\_CHECKED) { info.fState &=~MFS\_CHECKED; info.fState|=MFS\_UNCHECKED; \_tcscpy(buf,\_T("Menu is unchecked")); info.cch=\_tcslen(buf)+1; } else { info.fState &=~MFS\_UNCHECKED; info.fState |=MFS\_CHECKED; \_tcscpy(buf,\_T("Menu is checked")); info.cch=\_tcslen(buf)+1; } er=SetMenuItemInfo(hMenu,3,1,&info); ((CMainFrame\*)(AfxGetApp()->m\_pMainWnd))->DrawMenuBar();
As I click on the menu item I never see a check, nor changed text. However as I am stepping through the code debugging, I see that changes did take place. What am I doing wrong? Any help appreciated Henryk
You need to create a handler for
ON_UPDATE_COMMAND_UI
. The handler will have aCCmdUI
class pointer as its parameter using which you can check the menu item. Here is an article that does this - Using ON_UPDATE_COMMAND_UI with menu items and controls[^]«_Superman_» _I love work. It gives me something to do between weekends.
-
You need to create a handler for
ON_UPDATE_COMMAND_UI
. The handler will have aCCmdUI
class pointer as its parameter using which you can check the menu item. Here is an article that does this - Using ON_UPDATE_COMMAND_UI with menu items and controls[^]«_Superman_» _I love work. It gives me something to do between weekends.
Thanks! that works. I guess it is another disconnect in MFC :( .