Popup Menu ON_COMMAND

I know that Popup menus in MFC don't send ON_COMMAND messages right? I need to check the menu item whenever the user clicks on it still. How should I handle this?
steenpatAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

MelangeCommented:
Popup menus do send WM_COMMAND/OnCommand messages. Are you using TrackPopupMenu to display it? Just make sure that you are checking for the command in the correct command path.
0
steenpatAuthor Commented:
Ok. I have defined a popup menu however I am unable to assign it a resource ID with visual studio 2005. How do I handle that with ON_COMMAND in the message map then?
0
steenpatAuthor Commented:
No I'm not using trackpopup menu its simply a submenu item from the main menu on this application that I created in the resource editor. I want the user to be able to click on it with the mouse and add a Check next to it, but since it is does not have a resource ID for some stupid reason I can't handle it with ON_COMMAND in the message map.
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

MelangeCommented:
Are you talking about a sub menu item or a sub menu? For example, let's say you have the "File" menu and underneath it you have menu items and a sub menu called "more". Then in "more" you have additional menu items. Are you trying to add a "check" for the "more" sub menu or for an item inside the "more" sub menu. Given the simple diagram below, is the check mark by "More" or by "Other"?

File
-------
Open
More->
......Other
......Additional
Save
Print

0
MelangeCommented:
As a round about way of doing it, you can set the check for the "More" sub menu as listed above in this manner:

CMenu * pMenu = AfxGetMainWnd()->GetMenu();
CMenu * pSubMenu = pMenu->GetSubMenu(0-based index of top-level menu here);
pSubMenu->CheckMenuItem(0-based index of popup-menu here, MF_BYPOSITION | MF_CHECKED);

You can also do it a little more dynamically by adding an OnUpdate handler for the first menu item listed in the popup menu. This update handler will be called in two different ways - once for the popup menu itself and once for the actual menu item. When called for the popup menu the m_pSubMenu member inside CCmdUI will be set to the address of the popup menu otherwise it will be NULL. Then you can check the item in this way:

void docviewormainframeclass::OnUpdateFirstMenuItem( CCmdUI* pCmdUI )
{
    if( pCmdUI->m_pSubMenu != NULL )
        pCmdUI->m_pMenu->CheckMenuItem(pCmdUI->m_nIndex, MF_CHECKED | MF_BYPOSITION);
}

Use MF_UNCHECKED instead to remove the check mark.
0
steenpatAuthor Commented:
"Given the simple diagram below, is the check mark by "More" or by "Other"?

Check mark by More

>>>>> void docviewormainframeclass::OnUpdateFirstMenuItem( CCmdUI* pCmdUI )
{
    if( pCmdUI->m_pSubMenu != NULL )
        pCmdUI->m_pMenu->CheckMenuItem(pCmdUI->m_nIndex, MF_CHECKED | MF_BYPOSITION);
}

But won't OnUpdate be generated by other actions other than a mouse click? I only need 'More' checked when the user clicks it with the left mouse button.
0
MelangeCommented:
The OnUpdate will be called when ever the parent sub menu is first displayed (regardless of how - mouse, keyboard, etc.). i.e. it will be called when the "File" menu is expanded. Are you sure you want it to take action only when it is clicked? The normal "action" of a sub-menu is to expand the sub-menu when it is "selected" (regardless of method - keyboard, mouse, etc.). It is easily possible to detect when it is "selected" by overriding OnMenuSelect. But, this will happen simply by hovering over it with the mouse. A WM_MENUSELECT message is sent for every menu item that get's "selected" (I don't mean by simply highlighting in this case). For a normal menu item, it is followed by a corresponding WM_COMMAND message. For popups the default is to expand the sub-menu.

But, like I said, OnMenuSelect will be called either when you click the popup or after you hover over it for a bit. If there was a "command" ID associated with it, you would've been notified in the same circumstances as WM_COMMANDs are generated from the WM_MENUSELECT message.

Truly detecting when that popup is "clicked" and not merely selected can be done, but it is not easy. I think you have to ask yourself if it is what you really want. Or perhaps you just want to know when it is expanded/selected (see above)?
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
steenpatAuthor Commented:
Melange:
 
Yes I think it is just too much of a pain to check the popup by clicking on it because of the lack of a resource Id. I also have to update the popup by some acls as well with functions that also require a resource ID so this is too much of a pain to do it this way, I'm simply going to have to not use it as a popup and display the submenu items as regular menu items below it and use a menu separator.

Thanks for your help though.
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.