-
I've got my form minimized to the tray, but I can't figure out how to display a pop-up menu when the icon is right-clicked.
I can capture the click just fine; I just can't figure out where the menu resides (in the minimized form, in a new form, or somewhere else?).
Can someone enlighten me?
Thanks,
--Josh
-
<?>
Code:
'Building a popup menu
'1) Use the menu editor and create this menu
mnuPopUpMenuExample
....mnuShowMe
....mnuWorks
....mnuExit
2) Paste this code into the form window
Private Sub Form_Load()
'if you leave the menu invisible on building you don't need this
'line of code in the form load event
mnuPopUpMenuExample.Visible = False
End Sub
Private Sub Form_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
'puts popup in the middle of the application form
PopupMenu mnuPopUpMenuExample, 0, frmMain.ScaleWidth / 3, frmMain.ScaleHeight / 3
End If
End Sub
-
If you use GetCursorPos to find the pointer position, you can popup the menu over the tray icon when they click.
-
Using the Shell_NotifyIcon api function:
Code:
'Module code:
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
Public Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
ucallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Public VBGTray As NOTIFYICONDATA
'Form Declarations:
Private Sub GoSystemTray()
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
VBGTray.ucallbackMessage = WM_MOUSEMOVE
VBGTray.hIcon = Me.Icon
'tool tip text
VBGTray.szTip = Me.Caption & vbNullChar
Call Shell_NotifyIcon(NIM_ADD, VBGTray)
App.TaskVisible = False 'remove application from taskbar
Me.Hide
End Sub
'Form Code:
Private Sub Form_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
Static lngMsg As Long
Static blnFlag As Boolean
Dim result As Long
lngMsg = X / Screen.TwipsPerPixelX
If blnFlag = False Then
blnFlag = True
Select Case lngMsg
'right-click
Case WM_RBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
Case WM_LBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
End Select
blnFlag = False
End If
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then
Call GoSystemTray
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub
-
Thanks, guys.. (*why* do none of the tray icon examples reference PopupMenu()?)
You don't need to use GetCursor, if you just leave the defaults (e.g. PopupMenu (mnuExample, 0)) then it will automatically do it from the cursor position.
--Josh