• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

How to cause TrackPopUpMenu to return before the it's normal return conditions.

I have implemented logic to detect when a user clicks on a submenu on a popup menu that was created using TrackPopUpMenu.
Now I want to have this cause the Menu to close (and the function to return).
How can I do this ?
Thank in advance.
0
ElishaKlein
Asked:
ElishaKlein
  • 19
  • 13
  • +1
1 Solution
 
rushinmonkeyCommented:
have you tried DestroyMenu() ?
0
 
Roshan DavisCommented:
post ESCAPE keyboard event. Just a tricky way, might have some other solutions

use "keybd_event()"

// Simulate a key press
         keybd_event( VK_ESCAPE,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | 0,
                      0 );

      // Simulate a key release
         keybd_event( VK_ESCAPE,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                      0);
0
 
melodiesoflifeCommented:
Please try:

CMenu menu;
CPoint pos;
GetCursorPos(&pos);


menu.LoadMenu (IDR_YOURMENU);
CMenu* pContextMenu = menu.GetSubMenu (0);
pContextMenu->TrackPopupMenu (TPM_LEFTALIGN |TPM_LEFTBUTTON
                        |TPM_RIGHTBUTTON, pos.x,
                                                   pos.y,this);//AfxGetMainWnd ());
pContextMenu->DestroyMenu();


Hope this help.
0
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.

 
ElishaKleinAuthor Commented:
Ok Rosh,
Interesting problem here.
I have a menu with submenus with submenus .....
The goal is when the user clicks on a menu item with submenus do something (navigate to a web page)... and close the menu.
I keep track of the parent of each menu in a map.
I know which menu item is highlighted so I backtrack through parents in a loop till I get to the top and each time I simulate one escape press (good idea there).
The problem is after a small delay the submenu of the selected item appears - if the user clicks before this the I have one more escape than I need (and this screws up my logic to navigate to a web site since the escape cancles my navigation).
If he clicks after the submenu appears it works fine.
How can I either detect whether the submenu has appeared already or detect that the delay time has past (how long is it ?) or best if possible set the menu to open submenus without delay when highlighted.
Thanks a lot.
0
 
Roshan DavisCommented:
I think the similar problem will occure in IE also
I'm checking that...
0
 
ElishaKleinAuthor Commented:
No in IE when you click an item with a submenu it just opens the submenu. I need different behavior.
0
 
Roshan DavisCommented:
Just a wild guess

put a message peeking/dispatiching loop in the ESCAPE simulating program, and remove the WM_LBUTTON/UP messages without processing.

Rosh
0
 
ElishaKleinAuthor Commented:
Sorry I didn't follow your last comment.
0
 
ElishaKleinAuthor Commented:
Can you explain please ?
0
 
Roshan DavisCommented:
Just a gues
Something like this

MSG msg;

while ( your ESCAPE simulate loop )
{
      while ( ::PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE) )
      {
            if ( msg.message != WM_LBUTTONDOWN &&
                   msg.message != WM_LBUTTONUP )
            {
                  ::TranslateMessage(&msg);
                  ::DispatchMessage(&msg);
            }
      }
}

Rosh :)
0
 
ElishaKleinAuthor Commented:
I don't understand how it's related to WM_LBUTTONDOWN and WM_LBUTTONUP.
0
 
ElishaKleinAuthor Commented:
I think we have a misunderstanding. My only problem is how many times to simulate escape.
Simple example.
Menu Item 1 > Menu item 2
Menu Item 1 is an item in the main popup menu.
Menu Item 2 is an item in the submenu.
I am detecting the click on Menu item 1 to do something.
When the mouse hovers over Menu item 1 menu item 2 will apear (after a little while).
The problem is if the user clicks menu item 1 after the submenu opens i need to hit escape twice. If he clicks before the submenu appears I need to click escape only once.
How do I know ?
0
 
Roshan DavisCommented:
Now I understood.

Post a user defined message, from there do the keybd_event. So that will execute after showing the child popup menu.

0
 
ElishaKleinAuthor Commented:
Ok I'll try that. Thanks.
0
 
Roshan DavisCommented:
Good Luck :o)
0
 
ElishaKleinAuthor Commented:
I am trying to make this call from :
LRESULT CALLBACK CBandToolBarCtrl::MouseProc(
  int nCode,      // hook code
  WPARAM wParam,  // message identifier
  LPARAM lParam   // mouse coordinates
)

How do I know which HWND to post the message to ?
0
 
ElishaKleinAuthor Commented:
It's a static member function.
0
 
Roshan DavisCommented:
What "AfxGetMainWnd()->m_hWnd" ?

This will be your application's main window (MainFrame)

Rosh :)
0
 
ElishaKleinAuthor Commented:
It's not an MFC application !
0
 
ElishaKleinAuthor Commented:
Ok I found the HWND but the solution of posting a message didn't work. I think the submenu appears after a delay not when a message is handled.
Any other ideas ?
0
 
Roshan DavisCommented:
then you can assign one global member assign the hwnd value when you are creating the main window

Rosh :)
0
 
ElishaKleinAuthor Commented:
I already did that. But the message posting idea didn't help me. I still have the same problem.
Thanks for you help.
Any more ideas.
0
 
ElishaKleinAuthor Commented:
How about if I peek for escape when TrackPopUpMenu returns then wait for the extra escape to go before I do the navigation ?
How could I code that ?
0
 
ElishaKleinAuthor Commented:
Sorry I'm not mch of a windows programmer I'm more into ATL, COM ...
:)
0
 
Roshan DavisCommented:
Set a 500 milli second Timer, from there use the keybd_event
Don't forget to kill that timer. That you can do from the timer function itself
Rosh :)
0
 
Roshan DavisCommented:
Is that okay? I can provide code, if needed
0
 
ElishaKleinAuthor Commented:
I want to try removing the extra escape from the message loop after TrackPopUpMenu returns first. (That way I don't have a delay for the user).
0
 
ElishaKleinAuthor Commented:
How about this:
MSG msg;
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
if ( (msg.message != WM_WM_KEYDOWN) ||  (int)msg.wParam != VK_ESCAPE)
PostMessage...
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
if ( (msg.message != WM_WM_KEYUP) ||  (int)msg.wParam != VK_ESCAPE)
PostMessage...

Would that work ?
Can you help me with the syntax ?
0
 
Roshan DavisCommented:
keybd_event is sending the message, not posting, so that will not work.

Delay is the simple one, for that you can use Timer.
0
 
ElishaKleinAuthor Commented:
So I don't understand why the extra escape is canceling my navigation if it's handeled before the menu closes.
0
 
Roshan DavisCommented:
ESCAPE is the short-cut of *STOP* in IE
0
 
ElishaKleinAuthor Commented:
grrrrrrrrrrrrrrrrrr
keybd_event( VK_F10,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);

What a nice way to waste a day....
Thanks for your help :)
                         
0
 
Roshan DavisCommented:
Is that problem solved?
0
 
ElishaKleinAuthor Commented:
Yes thats the solution f10 - just once and it closes the menu.
0

Featured Post

Industry Leaders: 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!

  • 19
  • 13
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now