Results 1 to 3 of 3

Thread: System tray menu on right click?

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    System tray menu on right click?

    I have a security/information relay app that sends itself to the system tray just fine, letting me know that it's working correctly. However, at the time being, it just sits there and has the MouseOver ToolTip, but I can't call any sort of menu, and I'd like to. Any pointers how to do this?

    I'm using the Shell_NotifyIcon API for what I have so far. Here's my current code:
    VB Code:
    1. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long
    2.    
    3.     Private Type NOTIFYICONDATA
    4.         cbSize As Long
    5.         hWnd As Long
    6.         uId As Long
    7.         uFlags As Long
    8.         ucallbackMessage As Long
    9.         hIcon As Long
    10.         szTip As String * 64
    11.     End Type
    12.    
    13.     Private Const NIM_ADD = &H0
    14.     Private Const NIM_MODIFY = &H1
    15.     Private Const NIM_DELETE = &H2
    16.     Private Const NIF_MESSAGE = &H1
    17.     Private Const NIF_ICON = &H2
    18.     Private Const NIF_TIP = &H4
    19.    
    20.     Private Const WM_LBUTTONDBLCLK = &H203
    21.     Private Const WM_LBUTTONDOWN = &H201
    22.     Private Const WM_RBUTTONUP = &H205
    23.    
    24.     Dim NID As NOTIFYICONDATA
    25.  
    26.  
    27. Private Sub Form_Load()
    28.  
    29.     '///System Tray\\\
    30.  
    31.         With NID
    32.             .cbSize = Len(NID)
    33.             .hIcon = Me.Icon
    34.             .hWnd = Me.hWnd
    35.             .szTip = "Crazy Dave's Security Suite" & vbNullChar
    36.             .ucallbackMessage = WM_LBUTTONDBLCLK
    37.             .uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
    38.             .uId = 1&
    39.         End With 'Set the data
    40.        
    41.         Shell_NotifyIcon NIM_ADD, NID 'Add the Icon
    42.    
    43.     '\\\System Tray///
    44. End Sub

  2. #2
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: System tray menu on right click?

    Here's a module I use.
    It pretty well explains itself

    You may have to do some tweaking on the cut-n-paste parts

    VB Code:
    1. '
    2. '* The original appears to have been written by:
    3. '* Robert J. Reich  ([email protected])
    4. '* He does not appear to have placed a copyright on it
    5. '*
    6. '* I've changed it so that most of it's code can be run from this module
    7. '* I also removed some of the over verbos comments :)
    8. '*
    9. '* Special Note:5/26/2005, by JRP
    10. '* I've discovered that some controls, like SSTab can steal focus
    11. '*   away from the Form_MouseMove event.
    12. '* I've altered the module to fix the issue.
    13. '* If you are using one of the 'problem' controls, simply
    14. '*      use that control everywhere this module asks for ctrlX.
    15. '* See the other notes placed in this module for more info.
    16. '*
    17. '* Note:5/26/2005, by JRP
    18. '* Added a fix to prevent the tray popup menu while the form is visable
    19.  
    20. '//////////////////////////////////////////////////////////////////
    21. '//
    22. '//WARNING:         If you run this in the IDE, do NOT use the IDE-STOP
    23. '//                 button to end this program.  That will bypass the
    24. '//                 Form_Unload event which is neccisary to restore
    25. '//                 the system tray back to it's original state.
    26. '//////////////////////////////////////////////////////////////////
    27.  
    28. Option Explicit
    29.  
    30. '//These are the two API functions we'll need to use here.  The first
    31. '//is the one that really does the work.  The second function is used
    32. '//to take action when the user clicks on the mouse icon (restores the
    33. '//program and brings it to front of all other windows.)
    34. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _
    35.           (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    36. Public Declare Function SetForegroundWindow Lib "user32" _
    37.           (ByVal hWnd As Long) As Long
    38.  
    39.  
    40. '//UDT required by Shell_NotifyIcon API call
    41. Private Type NOTIFYICONDATA
    42.     cbSize As Long             '//size of this UDT
    43.     hWnd As Long               '//handle of the app
    44.     uId As Long                '//unused (set to vbNull)
    45.     uFlags As Long             '//Flags needed for actions
    46.     uCallBackMessage As Long   '//WM we are going to subclass
    47.     hIcon As Long              '//Icon we're going to use for the systray
    48.     szTip As String * 64       '//ToolTip for the mouse_over of the icon.
    49. End Type
    50.  
    51.  
    52. '//Constants required by Shell_NotifyIcon API call:
    53. Public Const NIM_ADD = &H0             '//Flag : "ALL NEW nid"
    54. Public Const NIM_MODIFY = &H1          '//Flag : "ONLY MODIFYING nid"
    55. Public Const NIM_DELETE = &H2          '//Flag : "DELETE THE CURRENT nid"
    56. Public Const NIF_MESSAGE = &H1         '//Flag : "Message in nid is valid"
    57. Public Const NIF_ICON = &H2            '//Flag : "Icon in nid is valid"
    58. Public Const NIF_TIP = &H4             '//Flag : "Tip in nid is valid"
    59. Public Const WM_MOUSEMOVE = &H200      '//This is our CallBack Message
    60. Public Const WM_LBUTTONDOWN = &H201    '//LButton down
    61. Public Const WM_LBUTTONUP = &H202      '//LButton up
    62. Public Const WM_LBUTTONDBLCLK = &H203  '//LDouble-click
    63. Public Const WM_RBUTTONDOWN = &H204    '//RButton down
    64. Public Const WM_RBUTTONUP = &H205      '//RButton up
    65. Public Const WM_RBUTTONDBLCLK = &H206  '//RDouble-click
    66.  
    67. Private nid As NOTIFYICONDATA       '//global UDT for the systray function
    68.  
    69. Public g_bIconSwitch As Boolean
    70.  
    71. Public g_MinimizeToTray As Boolean
    72.  
    73. '!!! NOTE!!!!
    74. 'Copy these REMed subs into your main Form!!!!
    75. 'Private Sub Form_Activate()
    76. '   InitalizeTray Me.hWnd, Me.Icon, "Cool, We're in the SysTray!"
    77. '   'NOTE!, if you're using a 'problem' control like SSTab, then use this line instead
    78. '   'InitalizeTray SSTab1, Me.Icon, "Cool, We're in the SysTray!"
    79. 'End Sub
    80. '
    81. '### !!!NOTE!!! ### If you attach to a control like SSTab
    82. '                   Change the name of the next sub
    83. '                   Example:
    84. '                   Private Sub SSTab_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    85. 'Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
    86. '                           X As Single, Y As Single)
    87. ''Purpose: This is the callback function of icon in the
    88. ''         system tray.  This is where it will process
    89. ''         what the application will do when Mouse Input
    90. ''         is given to the icon.
    91. '    Dim msg As Long     '//The callback value
    92. '
    93. '    'Prevent the popup window from opening if the
    94. '    'form is already visable
    95. '    If Me.Visible Then Exit Sub
    96. '    If (Me.ScaleMode = vbPixels) Then
    97. '        msg = X
    98. '    Else
    99. '        msg = X / Screen.TwipsPerPixelX
    100. '    End If
    101. '
    102. '    Select Case Msg
    103. ''        Case WM_LBUTTONUP, WM_LBUTTONDBLCLK '514 restore form window, single click
    104. ''            RestoreFromSysTray Me
    105. '        Case WM_LBUTTONDBLCLK                '515 restore form window, double click
    106. '                RestoreFromSysTray Me
    107. '        Case WM_RBUTTONUP                    '517 display popup menu
    108. '            Result = SetForegroundWindow(Me.hwnd)
    109. '            Me.PopupMenu Me.mPopupSys
    110. '    End Select
    111. 'End Sub
    112. '
    113. 'Private Sub Form_Resize()
    114. ''If the form is only minimized, it's icon will stay in the TaskBar
    115. ''So use this next line so that it only shows in the SysTray
    116. '   If (Me.WindowState = vbMinimized) Then Me.Hide
    117. 'End Sub
    118. '
    119. 'Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    120. '    KillTrayIcon
    121. '    Set Me = Nothing
    122. 'End Sub
    123.  
    124. 'Public Sub InitalizeTray(ByRef ctrlX As Control, TrayIcon As Picture, ByRef ToolTip As String)
    125. Public Sub InitalizeTray(ByRef hWnd As Long, TrayIcon As Picture, ByRef ToolTip As String)
    126. 'This uses the UTD to place your apps Icon in the SysTray
    127. ' the form must be fully visable.
    128. 'The Form_Activate Event is a perfect place for the call to this sub.
    129. 'NOTE: ctrlX can be your form or a control such as SSTab
    130.     With nid
    131.         .cbSize = Len(nid)
    132. '        .hWnd = ctrlX.hWnd
    133.         .hWnd = hWnd
    134.         .uId = vbNull
    135.         .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    136.         .uCallBackMessage = WM_MOUSEMOVE
    137.         .hIcon = TrayIcon
    138.         .szTip = ToolTip & vbNullChar
    139.     End With
    140.     Shell_NotifyIcon NIM_ADD, nid
    141. End Sub
    142.  
    143. Public Sub RestoreFromSysTray(frmX As Form)
    144. 'Call this sub after someone LeftClicks your tray Icon
    145. 'Or anytime you want to restore the program window
    146.     frmX.WindowState = vbNormal
    147.     Call SetForegroundWindow(frmX.hWnd)
    148.     frmX.Show
    149. End Sub
    150.  
    151. 'Public Sub ChangeToolTip(ctrlX As Control, ByRef NewTip As String)
    152. Public Sub ChangeToolTip(ByRef hWnd As Long, ByRef NewTip As String)
    153. 'Purpose:   Change the ToolTip of the System Tray Icon
    154. 'NOTE: ctrlX can be your form or a control such as SSTab
    155. '       Be sure to use the same control as you used in InitalizeTray
    156.   Dim nidNewTip As NOTIFYICONDATA     '//New ToolTip nid
    157.    
    158.   With nidNewTip
    159.     .cbSize = Len(nidNewTip)
    160. '    .hWnd = ctrlX.hWnd
    161.     .hWnd = hWnd
    162.     .uId = vbNull
    163.     .uFlags = NIF_TIP       '//Here the Tip is the only valid "new data"
    164.     .szTip = NewTip & vbNullChar
    165.   End With
    166.   Shell_NotifyIcon NIM_MODIFY, nidNewTip
    167. End Sub
    168.  
    169. 'Public Sub AutoSwitchIcons(ctrlX As Control, Icon1 As Picture, Icon2 As Picture)
    170. Public Sub AutoSwitchIcons(hWnd As Long, Icon1 As Picture, Icon2 As Picture)
    171. 'This lets you Swicth/flash between two Icons
    172. 'Just place the next call in you Timer sub/loop
    173. '   AutoSwitchIcons SSTab1, Me.Icon, Icon2
    174. 'NOTE: ctrlX can be your form or a control such as SSTab
    175. '       Be sure to use the same control as you used in InitalizeTray
    176.     Dim nidflash As NOTIFYICONDATA     '//New "Flashing" nid
    177.     Static SwitchIcons As Boolean      '//Flag to decide which icon to show
    178.    
    179.     If Not g_bIconSwitch Then Exit Sub
    180.     With nidflash
    181.         '//only thing we're really changing from
    182.         '//the original structure is the icon.
    183.         '//Hence the NIF_ICON flag.
    184.         .cbSize = Len(nidflash)
    185.         .hWnd = hWnd
    186.         .uId = vbNull
    187.         .uFlags = NIF_ICON
    188.         If (SwitchIcons) Then
    189.             '//we need to change it to the
    190.             '//"non-app" icon.
    191.             .hIcon = Icon2
    192.             SwitchIcons = False
    193.         Else
    194.             .hIcon = Icon1
    195.             SwitchIcons = True
    196.         End If
    197.     End With
    198.     Shell_NotifyIcon NIM_MODIFY, nidflash
    199. End Sub
    200.  
    201. 'Public Sub ChangeIcon(ctrlX As Control, NewIcon As Picture)
    202. Public Sub ChangeIcon(hWnd As Long, NewIcon As Picture)
    203. 'This let's you change the icon to whatwere you may want
    204. 'NOTE:
    205. '   You can also use it to make sure you have the original Icon
    206. '   after useing AutoSwitchIcons by using the next call:
    207. '   ChangeIcon Me, Me.Icon
    208. 'NOTE: ctrlX can be your form or a control such as SSTab
    209. '       Be sure to use the same control as you used in InitalizeTray
    210.     Dim nidChangeIcon As NOTIFYICONDATA     '//New "Flashing" nid
    211.    
    212.     With nidChangeIcon
    213.         '//We're only changing the Icon
    214.         '//Hence the NIF_ICON flag.
    215.         .cbSize = Len(nidChangeIcon)
    216.         .hWnd = hWnd
    217.         .uId = vbNull
    218.         .uFlags = NIF_ICON
    219.         .hIcon = NewIcon
    220.     End With
    221.     Shell_NotifyIcon NIM_MODIFY, nidChangeIcon
    222. End Sub
    223.  
    224. Public Sub KillTrayIcon()
    225. 'Call this from you Form's Unload event, before: Set ME = Nothing
    226.     Shell_NotifyIcon NIM_DELETE, nid
    227. End Sub

  3. #3
    New Member
    Join Date
    Oct 2003
    Location
    Valenzuela City, Philippines
    Posts
    9

    Re: System tray menu on right click?

    Quote Originally Posted by timeshifter
    I have a security/information relay app that sends itself to the system tray just fine, letting me know that it's working correctly. However, at the time being, it just sits there and has the MouseOver ToolTip, but I can't call any sort of menu, and I'd like to. Any pointers how to do this?

    I'm using the Shell_NotifyIcon API for what I have so far. Here's my current code:
    VB Code:
    1. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long
    2.    
    3.     Private Type NOTIFYICONDATA
    4.         cbSize As Long
    5.         hWnd As Long
    6.         uId As Long
    7.         uFlags As Long
    8.         ucallbackMessage As Long
    9.         hIcon As Long
    10.         szTip As String * 64
    11.     End Type
    12.    
    13.     Private Const NIM_ADD = &H0
    14.     Private Const NIM_MODIFY = &H1
    15.     Private Const NIM_DELETE = &H2
    16.     Private Const NIF_MESSAGE = &H1
    17.     Private Const NIF_ICON = &H2
    18.     Private Const NIF_TIP = &H4
    19.    
    20.     Private Const WM_LBUTTONDBLCLK = &H203
    21.     Private Const WM_LBUTTONDOWN = &H201
    22.     Private Const WM_RBUTTONUP = &H205
    23.    
    24.     Dim NID As NOTIFYICONDATA
    25.  
    26.  
    27. Private Sub Form_Load()
    28.  
    29.     '///System Tray\\\
    30.  
    31.         With NID
    32.             .cbSize = Len(NID)
    33.             .hIcon = Me.Icon
    34.             .hWnd = Me.hWnd
    35.             .szTip = "Crazy Dave's Security Suite" & vbNullChar
    36.             .ucallbackMessage = WM_LBUTTONDBLCLK
    37.             .uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
    38.             .uId = 1&
    39.         End With 'Set the data
    40.        
    41.         Shell_NotifyIcon NIM_ADD, NID 'Add the Icon
    42.    
    43.     '\\\System Tray///
    44. End Sub
    Check the mousedown event of your form
    when you click the systray icon, the mousedown event of the form triggers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width