Link to home
Start Free TrialLog in
Avatar of Bill Nolan
Bill NolanFlag for United States of America

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?
Avatar of Vinayak Kumbar
Vinayak Kumbar

Hi,

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
Avatar of Bill Nolan

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
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(CCmdUI* pCmdUI);
afx_msg void OnCommwObserve();
      In cpp file:

ON_UPDATE_COMMAND_UI(ID_COMMW_OBSERVE, OnUpdateCommwObserve)
ON_COMMAND(ID_COMMW_OBSERVE, OnCommwObserve)

And in the OnRButtonDown() handler:

CMenu aMenu;
aMenu.LoadMenu(IDR_COMMW_CONTEXT_MENU);
ClientToScreen(&point);                  
// Display popup at cursor position
aMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,      point.x, point.y, this);


And then I have the functions for each message.
ASKER CERTIFIED SOLUTION
Avatar of V_Bapat
V_Bapat
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Instead of RBUTTONDOWN message handler u r suposed to use the WM_CONTEXT menu as
void CContextvanishView::OnContextMenu(CWnd* pWnd, CPoint point)
{

      CMenu menu;
      menu.LoadMenu(IDR_MAINFRAME);
      m_pPopUp = menu.GetSubMenu(0);

      m_pPopUp->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
                point.x, point.y,
                  AfxGetMainWnd());

      
}

Try it. I will try and send u the sample for it.
VinExpert
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.