Bill Nolan
asked on
How can I update right-click context menus? (pCmdUI)
I have some context menus that popup when the user right clicks on various things. I would like to update this menu (set check marks,
etc) before it pops up, but I can't seem to get it to work. Does anyone know how to do this?
etc) before it pops up, but I can't seem to get it to work. Does anyone know how to do this?
ASKER
I'm not sure if I get you on this one. Are you saying to do this stuff in OnUpdate function? Because this is my problem- for right click context menus this function is not getting called. I don't know why.
HI,
It may be becuase of the IDs. The ID values which r there in Ur pop up menu should be mapped. Then it will work.
R U doing GetSubMenu(0); of some menu or u r creating it?. Post the related code so that I can help U.
VinExpert
It may be becuase of the IDs. The ID values which r there in Ur pop up menu should be mapped. Then it will work.
R U doing GetSubMenu(0); of some menu or u r creating it?. Post the related code so that I can help U.
VinExpert
ASKER
Here is the code. Note that the function OnCommwObserve() DOES get called, and OnUpdateCommwObserve() DOES NOT get called. There is NO difference in my implementation of these two, only the fact that the UI message is never processed.
In .h file:
afx_msg void OnUpdateCommwObserve(CCmdU I* pCmdUI);
afx_msg void OnCommwObserve();
In cpp file:
ON_UPDATE_COMMAND_UI(ID_CO MMW_OBSERV E, OnUpdateCommwObserve)
ON_COMMAND(ID_COMMW_OBSERV E, OnCommwObserve)
And in the OnRButtonDown() handler:
CMenu aMenu;
aMenu.LoadMenu(IDR_COMMW_C ONTEXT_MEN U);
ClientToScreen(&point);
// Display popup at cursor position
aMenu.GetSubMenu(0)->Track PopupMenu( TPM_LEFTAL IGN|TPM_RI GHTBUTTON, point.x, point.y, this);
And then I have the functions for each message.
In .h file:
afx_msg void OnUpdateCommwObserve(CCmdU
afx_msg void OnCommwObserve();
In cpp file:
ON_UPDATE_COMMAND_UI(ID_CO
ON_COMMAND(ID_COMMW_OBSERV
And in the OnRButtonDown() handler:
CMenu aMenu;
aMenu.LoadMenu(IDR_COMMW_C
ClientToScreen(&point);
// Display popup at cursor position
aMenu.GetSubMenu(0)->Track
And then I have the functions for each message.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Instead of RBUTTONDOWN message handler u r suposed to use the WM_CONTEXT menu as
void CContextvanishView::OnCont extMenu(CW nd* pWnd, CPoint point)
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAM E);
m_pPopUp = menu.GetSubMenu(0);
m_pPopUp->TrackPopupMenu(T PM_LEFTALI GN | TPM_RIGHTBUTTON,
point.x, point.y,
AfxGetMainWnd());
}
Try it. I will try and send u the sample for it.
VinExpert
Instead of RBUTTONDOWN message handler u r suposed to use the WM_CONTEXT menu as
void CContextvanishView::OnCont
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAM
m_pPopUp = menu.GetSubMenu(0);
m_pPopUp->TrackPopupMenu(T
point.x, point.y,
AfxGetMainWnd());
}
Try it. I will try and send u the sample for it.
VinExpert
ASKER
Thank you for your help VinExpert, and I learned a couple things. However, I have to give the points to V Bapat because he (sort of) answered my immediate question (i.e. how to modify the menus). The OnContextMenu function was useful to know, but it did not help me to change the menus.
Anyway, I needed to change the text in these menus, and it seems I can do so through CMenu.ModifyMenu(), just so you guys know.
I am still unsure why the standard ON_UPDATE_COMMAND_UI messages do not work for context menus. If anyone knows, I would be happy to hear it.
Thanks again.
Anyway, I needed to change the text in these menus, and it seems I can do so through CMenu.ModifyMenu(), just so you guys know.
I am still unsure why the standard ON_UPDATE_COMMAND_UI messages do not work for context menus. If anyone knows, I would be happy to hear it.
Thanks again.
I suppose, there r 3 menu items as
ID_ONE = one
ID_TWO = Two
ID_SIX = Six
Then popup the context menu as U do normally using TrackPopupMenu(...)
Have a variable say m_Checkit for that class and use it as
m_Checkit = 0 => check first menu option ID_ONE
m_Checkit = 1 => check second menu option ID_TWO
m_Checkit = 2 => check third menu option ID_SIX
U can set those values as U already know which one set and when to set.
Then map the UPDATE_COMMAND_UI functions for all menu items and do as
...::OnUpdateOne(CCmdUI* pCmdUI)
{
if(m_Checkit == 0)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
llly for second
...::OnUpdateOne(CCmdUI* pCmdUI)
{
if(m_Checkit == 1)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
llly for third.
Thats it. Hope this helps.
VinExpert