[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1594
  • Last Modified:

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?
0
steenpat
Asked:
steenpat
  • 4
  • 4
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now