by SKoW (11 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (30 Votes)
Simple code + explaination of why popup menus don't disapear like most app's menus when called from the system tray. Very hard problem, very simple answer. (this has no minimize to system tray code in it)
Fixing PopupMenu Fault when used in
a System Tray Project
If you have ever sent your app to the system tray and created a click event to popup a menu using Popupmenu (menuname) you will be aware of the small 'bug'.
By default, your application WILL NOT be set as the active app so when you call the popupmenu function, it will create the menu out of focus. This means it cannot recieve the 'lost focus' message and will not close when you click somewhere else. This is a very simple thing to fix but nobody seems to use it. All you have to do is set the Form owner of the menu to Focus :)
eg: (from standard sys tray form_mousemove event)
'(place in module)
Public Declare Function SetForegroundWindow
Lib "user32" (ByVal hwnd
As Long) As Long
'(place in form_mousemove event)
Private Sub Form_MouseMove(Button As
Integer, Shift As Integer, X
As Single, Y As
Single)
If Me.WindowState
= vbMinimized then
' window is minimized must be in system tray or MouseMove event would not
execute
Dim
lngMsg As Long
Dim result
As Long
' get the WM Message passed via X
' since X is by default mes. in Twips,
' devide it by the number of twips / pixel
' so we recieve the proper value
lngMsg = X / Screen.TwipsPerPixelX
Select Case lngMsg
case WM_RBUTTONUP ' right button
SetForegroundWindow Me.hwnd
Popupmenu Me.mnuFile
end select
end if
end sub
and it's that simple. Can't remember who told me this but thanks if it were ye. :)