Solved

How to programmatically Close/Hide a Popup menu?

Posted on 2015-01-15
12
407 Views
Last Modified: 2015-01-19
How to programmatically close/hide a popup menu, in VB6?
For instance, when a menu is shown with PopupMenu mnuFloat, it closes when a user clicks on the menus or somewhere outside the menus. However, how to close it without the need for user to click on the menus or somewhere outside the menus, i.e. completely programmatically?

REMARK: Calling SetForegroundWindow MenuParentForm.hWnd, where the MenyParentForm is the form on which the menu resides, although would work, from certain reasons in my project, is not a best solution, and I need an alternative way to close the menu, using API presumably…

PS: just to mention, an approach such as mnuFloat.Visible=False, mnuFloatSubmenu1.Visible=False etc., does not work…
0
Comment
Question by:npaun
  • 5
  • 5
  • 2
12 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40551544
I don't think it's possible. This is from Microsoft:

When you display a pop-up menu, the code following the call to the PopupMenu method isn't executed until the user either chooses a command from the menu (in which case the code for that command's Click event is executed before the code following the PopupMenu statement) or cancels the menu. In addition, only one pop-up menu can be displayed at a time; therefore, calls to this method are ignored if a pop-up menu is already displayed or if a pull-down menu is open.
0
 

Author Comment

by:npaun
ID: 40551618
Thank you for the comment.
That is partially true: for example, if you have a timer on a form where the menu resides, while the popup menu is shown, the code inside of the timer is not executed...

However, tests show that if you have a mouse hook in a public module, IT DOES work normally even while the menu is show, so it can catch for instance a mouse down event, which can trigger hiding of the menu, which I succeeded by using SetForegroundWindow MenuParentForm.hWnd. However, as I mentioned, that particular trick for closing a menu is not quite god for my current needs, so I would need an alternative, for Instance by calling an appropriate API for a menu with a known menu handle, or sending a message, or something like that....
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40551807
What about using SendKeys("{ESC}") to close the popup?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40551839
What about using SendKeys("{ESC}") to close the popup
The popup is modal so the code after it doesn't run until the menu is closed.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40551907
The popup is modal so the code after it doesn't run until the menu is closed.
Of course you don't want to close the menu in the code after it. What would be the point of opening it in the first place?
You would call SendKeys from somewhere else.
0
 

Author Comment

by:npaun
ID: 40554995
huh, well yes, but that might interfere with other application(s), and it is inelegant, not to mention that SendKeys is broken on Win7... I'm hopping to find a way to properly close a menu, there has be an API way to do it...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:louisfr
ID: 40555845
I tried the SendKeys solution on a Win7 and it worked.
But you're right. SendKeys would send the "escape" key to whatever window has the focus at the moment.

You could use the SendMessage function to send that keystroke to your window.
Declaration of the function and some constants:
Private const WM_KEYDOWN = &H100
Private const VK_ESCAPE = &H1B
Private Declare Function SendMessage Lib "user32" Alias _
 "SendMessageW"(ByVal hwnd As Long, ByVal wMsg As Long, _
 ByVal wParam As Long, lParam As Any) As Long

Open in new window

Using it to send Escape key to this Form (even if it doesn't have focus):
SendMessage(Me.Hwnd, WM_KEYDOWN, VK_ESCAPE, 0)

Open in new window

0
 

Author Comment

by:npaun
ID: 40556174
There are reported some problems with VB6 SendKeys on Win7: https://www.google.com/search?num=100&site=&source=hp&q=sendkeys+windows+7+vb6
There are workarounds, but it is better to be avoided. Doesn't matter...
Tried with SendMessage as you suggested, but it doesn't seem to work for me... it might be due to the fact that the menu's parent form is "hidden" (Visible=False, ShowInTaskbar=False), just as menu container for menus to be show, I don't know...
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40557127
I tried the WM_KEYDOWN message and it fails.
The WM_CANCELMODE message should be better.
Private Const WM_CANCELMODE = &H1F
SendMessage Me, WM_CANCELMODE, 0, 0

Open in new window

I tried it and it cancels the menu, even if the window is hidden.
0
 

Author Comment

by:npaun
ID: 40557206
ok, it works, Thanks!
One more thing I would need for this to be applied, if possible for this thread: how to know is the popup menu is currently displayed on the screen, supposedly based on some API call using the known hMenu handle?

(of course, it this could be partially done by directly setting an internal flag gMenuDisplayed =True when calling the PopupMenu, and then gMenuDisplayed=False inside the mnuMyPopupMenu_Click, but it is not a complete solution, as in principle the popup menu can disappear either by user clicking the menu or clicking outside of it. I would need a more direct information for that,  for instance as a reading from the menu structure itself...)
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40557211
Set a flag before you call Popup, and unset the flag after the call.
As stated several times above, the instruction after the call will only be reached after the menu is closed.
0
 

Author Comment

by:npaun
ID: 40557243
yes, you are of course right, setting flag to false inside mnuMyPopupMenu_Click would be wrong, the code is halted and resumed when the menu is closed regardless of the way it is closed, by directly clicking on it, shifting focus to another window/app or clicking outside the menu... I jumped ahead and skipped to utilize that...

Thank you very much for the help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA Check for Character 9 69
DO Loop not working 4 63
JSON Response and request in VB6 application 11 136
Visual Studio 2005 text editor 10 20
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now