Discussion:
Programmatically Expand or Collapse a sub menu
(too old to reply)
Mike Arsenault
2006-02-22 16:57:31 UTC
Permalink
Does anyone know how to do this on a CMenu object?

I'm working on a RClick feature for a CMenu. I've got to the point now
where, while the menu is displayed, I can Right click on a menu item
then that item will automatically be converted to a SubMenu and the
little arrow on the right side appears - this all happens automatically
while the menu is still displayed.

Problem is that I have to move the mouse over, in order to display the
new submenu. I would like for the submenu to be displayed
automatically, after I've added it.

Is there a hidden method on CMenu that I can't see, or perhaps a
windows message that I can send to something in order to automatically
expand the sub menu?

Thanks in advance for any help

Mike
Ajay Kalra
2006-02-22 17:39:33 UTC
Permalink
I can Right click on a menu item then that item will automatically be converted to a SubMenu
Just curious, how did you accomplish this since CMenu is derived from
CWnd?

If I am not missing what you are asking, all you need is
CMenu::TrackPopupMenu.

---------
Ajay Kalra
***@yahoo.com
Mike Arsenault
2006-02-22 18:08:43 UTC
Permalink
That would be the obvious answer - so I tried that :-) But it turns out
that TrackPopupMenu fails because I already have a PopupMenu displayed.
THat is the exact behavior that I want so I'm trying a little trick
where I'll dynamically add a submenu to the item that I've Right
clicked on.
Ajay Kalra
2006-02-22 18:23:26 UTC
Permalink
How about selecting the menu item programatically. That should pop up
the menu. I dont know the best way to select an item though.

-------
Ajay Kalra
***@yahoo.com
Mike Arsenault
2006-02-22 18:38:05 UTC
Permalink
I never thought of that - it does sound like a good idea, but I'm not
sure I know of a function on CMenu that will do that??
Ajay Kalra
2006-02-22 19:19:16 UTC
Permalink
Unfortunately CMenu does not have any method that will let you select
an item. One way would be to move the mouse (SetCursorPos) to the
location of desired item. I still find it hard to believe that this
needs to be done in order to achieve this. It should be straightforward
using TrackPopupMenu etc.

------
Ajay Kalra
***@yahoo.com
unknown
2006-02-22 22:59:34 UTC
Permalink
Post by Ajay Kalra
How about selecting the menu item programatically. That should pop up
the menu. I dont know the best way to select an item though.
Interesting, I tried WM_MENUCOMMAND and WM_MENUSELECT without luck.
Obviously I couldn't use WM_COMMAND because there's no ID for a popup.

Not a sausage on either one. Tricky.

--
Bob Moore
http://bobmoore.mvps.org/
(this is a non-commercial site and does not accept advertising)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ajay Kalra
2006-02-23 03:09:38 UTC
Permalink
I am surprised that this has not been brought up so far. I would have
expected some API which allows menu to be programmatically manipulated
from user interaction point of view. I am not sure why Win32 does not
expose the window object that contains the menu.

------
Ajay Kalra
***@yahoo.com
AliR
2006-02-22 20:20:46 UTC
Permalink
I am a bit lost, but I think I see what you are doing. You have a menu say
file and under that you have a bunch of items. So you are loading the menu
and then calling TrackPopupMenu. Where you should be getting the submenu
first then calling TrackPopupMenu on that.

CMenu Menu;
Menu.LoadMenu(IDR_MYPOPUPMENU);
CMenu *pSubMenu = Menu.GetSubMenu(0);
pSubMenu->TrackPopupMenu();

AliR.
Post by Mike Arsenault
Does anyone know how to do this on a CMenu object?
I'm working on a RClick feature for a CMenu. I've got to the point now
where, while the menu is displayed, I can Right click on a menu item
then that item will automatically be converted to a SubMenu and the
little arrow on the right side appears - this all happens automatically
while the menu is still displayed.
Problem is that I have to move the mouse over, in order to display the
new submenu. I would like for the submenu to be displayed
automatically, after I've added it.
Is there a hidden method on CMenu that I can't see, or perhaps a
windows message that I can send to something in order to automatically
expand the sub menu?
Thanks in advance for any help
Mike
Ajay Kalra
2006-02-22 20:27:10 UTC
Permalink
This works but I think what OP wants is to continue showing the parent
menu of this submenu as well. By calling TrackPopupMenu on
GetSubMenu(0), the parent menu disappears. I am guessing parent menu in
this case is also a popup menu.

------------
Ajay Kalra
***@yahoo.com
AliR
2006-02-22 20:30:25 UTC
Permalink
I thought that too, but why would you want to popup a menu and open it's
submenu at the same time?

AliR.
Post by Ajay Kalra
This works but I think what OP wants is to continue showing the parent
menu of this submenu as well. By calling TrackPopupMenu on
GetSubMenu(0), the parent menu disappears. I am guessing parent menu in
this case is also a popup menu.
------------
Ajay Kalra
Ajay Kalra
2006-02-22 20:38:15 UTC
Permalink
why would you want to popup a menu and open it's submenu at the same time?
It does not seem odd to me. Win32 lets you do it. It appears it does
not let you do it programmatically.

----------
Ajay Kalra
***@yahoo.com
Mike Arsenault
2006-02-22 21:50:26 UTC
Permalink
Doesn't matter any more - Canada just lost the hockey game :-( -
Nothing matters any more, specially CMenu's :-)
Mike Arsenault
2006-02-22 20:53:06 UTC
Permalink
WOW - thanks for all the comments!!

AliR, I have attempted calling TrackPopupMenu, but as I mentioed above,
I get error code 1446 from GetLastError which is
ERROR_POPUP_ALREADY_ACTIVE.

The behavior that I'm trying to emulate is exhibited in IE. Click on
the Favorites menu of IE to display your saved favorites. RIght click
on one of those and, lo and behold, you get a context (popup menu) for
the menu item that you right clicked on.

THis is what I want to do - now I'm aware that the favorites menu on IE
is actually a WIndow that is programmed to look a menu and that is how
it works. If I was given infinte time, then I would probably take this
approach too, but I don't .... so I'm not :-).

I was hoping to take a quick attempt at this by dynamically and
programatically, convert the menu (that I right clicked on) into a sub
menu and <<sort of>> make it look the same.
Ajay Kalra
2006-02-22 20:57:42 UTC
Permalink
Actually the part of favorites menu that you are talking about is a
win32 menu. I am guessing MSFT hacked. But.. you mentioned that you
were able to look for right click on the menu. How did you achieve
that?

--------
Ajay Kalra
***@yahoo.com
Mike Arsenault
2006-02-22 21:04:42 UTC
Permalink
ON My frame class, I've written a handler for the WM_MENURBUTTONUP
message - the wParam is the position of the item Right-clicked on and
the lParam is the handle of the menu containing the item.

ASsuming that I'm able to get anywhere with this, I have to be able to
do the same thing from various popup context menus in out application
as well - I'm not sure if the handler for that case would also be on
the frame or some other location - but I'll cross that bridge when I
get to it.....

Mike
Ajay Kalra
2006-02-22 21:08:58 UTC
Permalink
Interesting.. I never thought of doing it this way. Actually a menu is
a special window except that its not exposed directly by MSFT and
surely not in MFC. So if you are able to catch the right click on the
menu in the frame, its kind of odd.

--------
Ajay Kalra
***@yahoo.com
AliR
2006-02-22 21:09:40 UTC
Permalink
I am confused here, when I right click on an item in the favorites in IE, it
opens a popup menu.
Open
Create new folder
Print
-------------------
Make available Offline
-------------------
Send To >
---------------
copy and so on

Am I missing something?

AliR.
Post by Ajay Kalra
Actually the part of favorites menu that you are talking about is a
win32 menu. I am guessing MSFT hacked. But.. you mentioned that you
were able to look for right click on the menu. How did you achieve
that?
--------
Ajay Kalra
Ajay Kalra
2006-02-22 21:24:31 UTC
Permalink
What you are saying is correct. How would you do this programmatically?
You have to keep all the menus open. IOW, Favorites pop up menu and the
SendTo popup menu as well.

Perhaps we are now going in different directions.

--------
Ajay Kalra
***@yahoo.com
AliR
2006-02-22 21:28:35 UTC
Permalink
Oh I think I am getting it now. You want a popup menu after right clicking a
normal.

Let me think about that for a minute.

AliR.
Post by Ajay Kalra
What you are saying is correct. How would you do this programmatically?
You have to keep all the menus open. IOW, Favorites pop up menu and the
SendTo popup menu as well.
Perhaps we are now going in different directions.
--------
Ajay Kalra
Mike Arsenault
2006-02-22 21:32:10 UTC
Permalink
AliR, Yes that works from the Favorites menu - but try it from the File
menu of IE - you'll notice that it doesn't work there - or even on the
top portion of the Favorites menu - that doesn't work either!! THe same
is true for a menu from the Word application.

I did receive confirmation from someone that the Favorites Menu in IE
is a bit of a special beast - it is a custom window.

Mike
AliR
2006-02-22 21:25:08 UTC
Permalink
Run this right click anywhere inside the dialog box, and then tell me the
difference between what I have and what you want to do.

http://www.learnstar.com/AliR/Rightclickmenu.exe


AliR.
Post by AliR
I am confused here, when I right click on an item in the favorites in IE, it
opens a popup menu.
Open
Create new folder
Print
-------------------
Make available Offline
-------------------
Send To >
---------------
copy and so on
Am I missing something?
AliR.
Post by Ajay Kalra
Actually the part of favorites menu that you are talking about is a
win32 menu. I am guessing MSFT hacked. But.. you mentioned that you
were able to look for right click on the menu. How did you achieve
that?
--------
Ajay Kalra
Ajay Kalra
2006-02-22 21:34:21 UTC
Permalink
Right clicking simply makes the menu disappear except when you right
click on "has children". From what I understand OP is adding the sub
menu "Has Children" dynamically and would like it to pop up without a
click.

Actually, I am now lost. Also time to go home. I will catch up later at
night.

------
Ajay Kalra
***@yahoo.com
AliR
2006-02-22 22:09:15 UTC
Permalink
Here is what I have so far. I have a CMenu and I changed it's parent, to
another window that I have control over. Now I am catching the
WM_MENURBUTTONUP message, there I am tried to do a TrackPopupMenu, but I am
getting an error 1446 (Popup menu already active) If I can find a way around
this, I think the problem will be solved.

AliR.
Post by Ajay Kalra
Right clicking simply makes the menu disappear except when you right
click on "has children". From what I understand OP is adding the sub
menu "Has Children" dynamically and would like it to pop up without a
click.
Actually, I am now lost. Also time to go home. I will catch up later at
night.
------
Ajay Kalra
AliR
2006-02-22 23:06:58 UTC
Permalink
Problem solved. I think.

Well I totally missed it in the Doc's for WM_MENURBUTTONUP, you have to call
TrackPopupMenu when the TPM_RECURSE flag.

That did it.

See if you like this, if you do I'll send you the code. Rigth click on the
dialog to get the right click menu, then right click on the menu item.

http://www.learnstar.com/AliR/Rightclickmenu.exe

AliR.
Post by AliR
Here is what I have so far. I have a CMenu and I changed it's parent, to
another window that I have control over. Now I am catching the
WM_MENURBUTTONUP message, there I am tried to do a TrackPopupMenu, but I am
getting an error 1446 (Popup menu already active) If I can find a way around
this, I think the problem will be solved.
AliR.
Post by Ajay Kalra
Right clicking simply makes the menu disappear except when you right
click on "has children". From what I understand OP is adding the sub
menu "Has Children" dynamically and would like it to pop up without a
click.
Actually, I am now lost. Also time to go home. I will catch up later at
night.
------
Ajay Kalra
Mike Arsenault
2006-02-23 14:33:50 UTC
Permalink
AliR, what can I say!!!! - That is amazing!! - It's funnny that I can't
find any reference to TPM_RECURSE in the help file and very little on
the Web. - I opened up the header file and determined that it's only
supported for WINVER >= 0x5000, which shouldn't be a big issue for us.

Listen, thanks a lot for your perseverence in tackling this problem -
We really appretiate it.

Where are you? Maybe I can buy you a beer someday!!!! :-)

Mike
AliR
2006-02-23 15:46:10 UTC
Permalink
I am in Dallas, though I would appreciate a good beer on any day, you don't
really have to do that. I am glad this helped you out.


AliR.
Post by Mike Arsenault
AliR, what can I say!!!! - That is amazing!! - It's funnny that I can't
find any reference to TPM_RECURSE in the help file and very little on
the Web. - I opened up the header file and determined that it's only
supported for WINVER >= 0x5000, which shouldn't be a big issue for us.
Listen, thanks a lot for your perseverence in tackling this problem -
We really appretiate it.
Where are you? Maybe I can buy you a beer someday!!!! :-)
Mike
Alexander Grigoriev
2006-02-23 16:38:45 UTC
Permalink
A reference to TPM_RECURSE is somewhat hidden in TrackPopupMenu SDK topic.
It's not visible at the first sight.
Post by Mike Arsenault
AliR, what can I say!!!! - That is amazing!! - It's funnny that I can't
find any reference to TPM_RECURSE in the help file and very little on
the Web. - I opened up the header file and determined that it's only
supported for WINVER >= 0x5000, which shouldn't be a big issue for us.
Listen, thanks a lot for your perseverence in tackling this problem -
We really appretiate it.
Where are you? Maybe I can buy you a beer someday!!!! :-)
Mike
Ajay Kalra
2006-02-23 14:46:28 UTC
Permalink
Excellent.

--------
Ajay Kalra
***@yahoo.com
Mike Arsenault
2006-02-22 22:53:00 UTC
Permalink
Sorry - I just realized that you posted a sample AliR. Thank you!!

I agree with what Ajay said, when right clicking on any action the menu
disappears. I would like it, instead, to popup a context menu.
Obviously click on the left button will and should invoke the menu
action, but the right button should display another menu.

Mike
AliR
2006-02-22 23:29:47 UTC
Permalink
Here is another example this one is an SDI app. You can open the File menu
and right click on the Menu items to bring up the popup menu. I have it to
where it will only do it on the file menu. And it also does nothing on the
New command.

http://www.learnstar.com/AliR/SDIPopupMenu.exe

The solution ended up simply being this

LRESULT CMainFrame::OnMenuRButtonUp(WPARAM wParam,LPARAM lParam)
{
//only do this when the are clicking on the File menu
CMenu *pMenu = GetMenu();
ASSERT(pMenu);
//get the File Menu
CMenu *pSubMenu = pMenu->GetSubMenu(0);
if (pSubMenu != NULL && (HMENU)lParam == pSubMenu->GetSafeHmenu())
{
//get cursor position, the docs say that wParam should have the point
//but it doesn't
CPoint Pt;
GetCursorPos(&Pt);

int MenuItem = MenuItemFromPoint(m_hWnd,pSubMenu->m_hMenu,Pt);
//don't do this for New menu item
if (MenuItem != 0)
{
CMenu Menu;
Menu.LoadMenu(IDR_MENU2);
CMenu *pPopupMenu = Menu.GetSubMenu(0);

pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RECURSE,Pt.x,Pt.
y,this);
}
}
return 1;
}

AliR.
Post by Mike Arsenault
Sorry - I just realized that you posted a sample AliR. Thank you!!
I agree with what Ajay said, when right clicking on any action the menu
disappears. I would like it, instead, to popup a context menu.
Obviously click on the left button will and should invoke the menu
action, but the right button should display another menu.
Mike
Alexander Grigoriev
2006-02-23 04:00:37 UTC
Permalink
TPM_RECURSE
Post by Mike Arsenault
Sorry - I just realized that you posted a sample AliR. Thank you!!
I agree with what Ajay said, when right clicking on any action the menu
disappears. I would like it, instead, to popup a context menu.
Obviously click on the left button will and should invoke the menu
action, but the right button should display another menu.
Mike
Alexander Grigoriev
2006-02-23 04:00:01 UTC
Permalink
Try TrackPopupMenu with TPM_RECURSE flag.
Post by Mike Arsenault
Does anyone know how to do this on a CMenu object?
I'm working on a RClick feature for a CMenu. I've got to the point now
where, while the menu is displayed, I can Right click on a menu item
then that item will automatically be converted to a SubMenu and the
little arrow on the right side appears - this all happens automatically
while the menu is still displayed.
Problem is that I have to move the mouse over, in order to display the
new submenu. I would like for the submenu to be displayed
automatically, after I've added it.
Is there a hidden method on CMenu that I can't see, or perhaps a
windows message that I can send to something in order to automatically
expand the sub menu?
Thanks in advance for any help
Mike
Loading...