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?
LVL 7
Bill NolanOwner, Lead Technology ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:
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
0
Bill NolanOwner, Lead Technology ProgrammerAuthor Commented:
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.
0
Vinayak KumbarSr Program ManagerCommented:
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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Bill NolanOwner, Lead Technology ProgrammerAuthor Commented:
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.
0
V_BapatCommented:
You can check/uncheck the menu items before you call TrackPopupMenu(...).
Suppose the ID of the menu items are ID_ITEM_ONE, ID_ITEM_TWO and ID_ITEM_THREE, you can check/uncheck these items like this:

aMenu.CheckMenuItem(ID_ITEM_ONE, MF_CHECKED);
aMenu.CheckMenuItem(ID_ITEM_TWO, MF_UNCHECKED);
aMenu.CheckMenuItem(ID_ITEM_THREE, MF_CHECKED);

aMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);

This will check item 1 & 3 and uncheck menu item 2.
That's all to it.

You can also enable/disable the items here. Look in MSDN for CMenu.

Vicky
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vinayak KumbarSr Program ManagerCommented:
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
0
Bill NolanOwner, Lead Technology ProgrammerAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.