Results 1 to 2 of 2

Thread: Your Form in Sys Tray...

Threaded View

  1. #1

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Your Form in Sys Tray...

    Ok, I have decided to post this in the code bank since I have been posting this in more then a few posts lately.

    This code show you how to make your form show in the system tray using the Shell_NotifyIcon API.

    Befor Using this code keep in mined that:
    • There is systray icon menu named "TrayMenu"
    • Exit button in that menu named "Ex"
    • A Show button in that menu named "Show"

    I have attached the example file of this exact code for better help. Check bottom of my post.
    VB Code:
    1. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    2. 'declaration for putting the application to system tray
    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.     'declare constants
    14. Private Const NIM_ADD = &H0
    15. Private Const NIM_MODIFY = &H1
    16. Private Const NIM_DELETE = &H2
    17. Private Const WM_MOUSEMOVE = &H200
    18. Private Const NIF_MESSAGE = &H1
    19. Private Const NIF_ICON = &H2
    20. Private Const NIF_TIP = &H4
    21.  
    22. 'The following constants are used to determine the mouse input on the
    23.  
    24.     'Left-click
    25. Private Const WM_LBUTTONDBLCLK = &H203   'Double click
    26. Private Const WM_LBUTTONDOWN = &H201     'down
    27. Private Const WM_LBUTTONUP = &H202       'up
    28.  
    29.     'Right-click
    30. Private Const WM_RBUTTONDBLCLK = &H206   'Double click
    31. Private Const WM_RBUTTONDOWN = &H204     'down
    32. Private Const WM_RBUTTONUP = &H205       'up
    33.  
    34. 'Dimension a variable as the user-defined data type.
    35. Dim nid As NOTIFYICONDATA
    36.  
    37.  
    38. Private Sub Ex_Click()
    39. Unload Me
    40. Shell_NotifyIcon NIM_DELETE, nid 'Delete the form's icon from the sys tray
    41. End Sub
    42. '   one main menu by Caption"Menu" name "TrayMenu"
    43. '   one sub menu by name caption "Exit" name "Ex"
    44. '   one sub menu by name caption "Show" name "Show"
    45.  
    46. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    47. Dim msg As Long
    48. Dim sFilter As String
    49. msg = X / Screen.TwipsPerPixelX
    50.     Select Case msg
    51.         Case WM_LBUTTONDOWN
    52.             MsgBox "you clicked me!"
    53.         '      Me.PopupMenu TrayMenu   'if u want u can call the popup menu
    54.         Case WM_LBUTTONUP
    55.         Case WM_LBUTTONDBLCLK
    56.         Case WM_RBUTTONDOWN
    57.         Case WM_RBUTTONUP
    58.             Me.PopupMenu TrayMenu  'call the popup menu
    59.         Case WM_RBUTTONDBLCLK
    60.     End Select
    61. End Sub
    62.  
    63.  
    64. Private Sub Form_Resize()
    65. If Me.WindowState = 1 Then
    66.  
    67.     Me.Hide
    68.     nid.cbSize = Len(nid)
    69.         nid.hwnd = Me.hwnd
    70.         nid.uId = vbNull
    71.         nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    72.         nid.uCallBackMessage = WM_MOUSEMOVE
    73.         nid.hIcon = Me.Icon
    74.         'u can give a tool tip text here
    75.         nid.szTip = "My System Tray Message" & vbNullChar
    76.        'Call the Shell_NotifyIcon function to add the icon to the System Tray, defulte form's icon
    77.  
    78.         Shell_NotifyIcon NIM_ADD, nid
    79.         Else
    80. End If
    81. End Sub
    82.  
    83. Private Sub Show_Click()
    84. Me.WindowState = 0
    85. Me.Show
    86. Shell_NotifyIcon NIM_DELETE, nid 'Delete the form's icon from the sys tray
    87. End Sub
    Attached Files Attached Files
    Last edited by wiz126; Dec 8th, 2005 at 06:44 PM.
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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