How to UPDATE_COMMAND_UI in Dialog App?
-
I added a menu to a dialog application, and can't seem to get the UPDATE_COMMAND_UI behavior to work. If I break on my OnUpdate... handler, it's called after the menu item has been selected, not before. I've been poking around on Code Project, and I'm starting to get the "impression" that it might not be possible. Is that true, or is there a way to update a menu item in a dialog app?
-
I added a menu to a dialog application, and can't seem to get the UPDATE_COMMAND_UI behavior to work. If I break on my OnUpdate... handler, it's called after the menu item has been selected, not before. I've been poking around on Code Project, and I'm starting to get the "impression" that it might not be possible. Is that true, or is there a way to update a menu item in a dialog app?
One solution is to implement OnInitMenuPopup(). ----- void CDlgApp::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) { // Enables this dialog based app to handle ON_UPDATE_COMMAND_UI messages. // For updating items in menu. CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu); CCmdUI cmdUI; cmdUI.m_pMenu = pPopupMenu; cmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount(); for (cmdUI.m_nIndex = 0; cmdUI.m_nIndex < cmdUI.m_nIndexMax; cmdUI.m_nIndex++) { cmdUI.m_nID = pPopupMenu->GetMenuItemID(cmdUI.m_nIndex); if (cmdUI.m_nID == 0) continue; cmdUI.DoUpdate(this, FALSE); } } ----- Kuphryn
-
One solution is to implement OnInitMenuPopup(). ----- void CDlgApp::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) { // Enables this dialog based app to handle ON_UPDATE_COMMAND_UI messages. // For updating items in menu. CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu); CCmdUI cmdUI; cmdUI.m_pMenu = pPopupMenu; cmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount(); for (cmdUI.m_nIndex = 0; cmdUI.m_nIndex < cmdUI.m_nIndexMax; cmdUI.m_nIndex++) { cmdUI.m_nID = pPopupMenu->GetMenuItemID(cmdUI.m_nIndex); if (cmdUI.m_nID == 0) continue; cmdUI.DoUpdate(this, FALSE); } } ----- Kuphryn
Thanks, I'll give it a try.
-
One solution is to implement OnInitMenuPopup(). ----- void CDlgApp::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) { // Enables this dialog based app to handle ON_UPDATE_COMMAND_UI messages. // For updating items in menu. CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu); CCmdUI cmdUI; cmdUI.m_pMenu = pPopupMenu; cmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount(); for (cmdUI.m_nIndex = 0; cmdUI.m_nIndex < cmdUI.m_nIndexMax; cmdUI.m_nIndex++) { cmdUI.m_nID = pPopupMenu->GetMenuItemID(cmdUI.m_nIndex); if (cmdUI.m_nID == 0) continue; cmdUI.DoUpdate(this, FALSE); } } ----- Kuphryn
I get an Access Violation when executing pPopupMenu->GetMenuItemCount() in your example. Here's where it occurs... _AFXWIN_INLINE UINT CMenu::GetMenuItemCount() const { ASSERT(::IsMenu(m_hMenu)); return ::GetMenuItemCount(m_hMenu); }
-
I get an Access Violation when executing pPopupMenu->GetMenuItemCount() in your example. Here's where it occurs... _AFXWIN_INLINE UINT CMenu::GetMenuItemCount() const { ASSERT(::IsMenu(m_hMenu)); return ::GetMenuItemCount(m_hMenu); }
It looks like pPopupMenu is not setup properly prior to the call to OnInitMenuPopup. I tried executing the following command... pPopupMenu->GetMenuState(0,MF_BYPOSITION); and it also results in an access violation. Oh Well. An Ideas?
-
I added a menu to a dialog application, and can't seem to get the UPDATE_COMMAND_UI behavior to work. If I break on my OnUpdate... handler, it's called after the menu item has been selected, not before. I've been poking around on Code Project, and I'm starting to get the "impression" that it might not be possible. Is that true, or is there a way to update a menu item in a dialog app?
Add a handler for
WM_KICKIDLE
. In the handler, callUpdateDialogControls(this, false);
You'll need to#include <afxpriv.h>
for the definition ofWM_KICKIDLE
--Mike-- Yeah, payin' the bills with my mad programming skillz. Defraggin' my hard drive for thrills. Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me -
Add a handler for
WM_KICKIDLE
. In the handler, callUpdateDialogControls(this, false);
You'll need to#include <afxpriv.h>
for the definition ofWM_KICKIDLE
--Mike-- Yeah, payin' the bills with my mad programming skillz. Defraggin' my hard drive for thrills. Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to meSeems like a losing battle... The ON_UPDATE_COMMAND_UI message handler is still never called. Oh well.
-
I added a menu to a dialog application, and can't seem to get the UPDATE_COMMAND_UI behavior to work. If I break on my OnUpdate... handler, it's called after the menu item has been selected, not before. I've been poking around on Code Project, and I'm starting to get the "impression" that it might not be possible. Is that true, or is there a way to update a menu item in a dialog app?
Look for 'Q242577' in MSDN, there is an example 80 lines of code overriding the OnInitMenuPopup function and it worked for me.
-
Look for 'Q242577' in MSDN, there is an example 80 lines of code overriding the OnInitMenuPopup function and it worked for me.
Hey! That did the trick, thanks.