Advertisement
2002ASP Miscellaneous #197

Drop Menu for icon in System Tray

Easy Code here: Place an icon in the System Tray and have a drop menu appear when you click the icon with the right-mouse button. If you already have code with bitmaps in your menu, just add this code to your project! This code DOES WORK, just be careful and follow the tips. Then slap yourself for other people's long and drawn out useless code from the past.

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
Private Sub Form_Load()
' Project Topic:
' "Add Menu to System Tray Icon"
' For VB5.0 and better....
' Created by opus@bargainbd.com
' Original source is unknown
' Before you begin!
' Make sure your form is in view within Visual Basic,
' then press Ctrl+E to open the Menu Editor.
' Next create a Main Menu item and make it's name
' property "mnu_1", without the quotes. You can
' always change this name, but make sure that you
' change it in the Form_MouseMove too. Now create a
' few sub menus under the main menu
' and name them anything that you want,
' the code will take care of the rest.
' "TIP: Make the "mnu_1" visible property = False
' Then create a second Main menu item with sub menus
' as normal (This will appear to look as though
' it is the first menu item. The Actual First
' will be seen in the System tray when clicked with
' the right mouse button.
 
 


' *---The code begins here---*
'The form must be fully visible before calling Shell_NotifyIcon
Me.Show
Me.Refresh
 
With nid
    .cbSize = Len(nid)
    .hwnd = Me.hwnd
    .uId = vbNull
    .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    .uCallBackMessage = WM_MOUSEMOVE
    .hIcon = Me.Icon
    .szTip = " Click Right Mouse Button " & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'This procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'The value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
 msg = X
Else
 msg = X / Screen.TwipsPerPixelX
End If
  Select Case msg
    Case WM_LBUTTONUP    '514 restore form window
     Me.WindowState = vbNormal
     Result = SetForegroundWindow(Me.hwnd)
     Me.Show
    Case WM_LBUTTONDBLCLK  '515 restore form window
     Me.WindowState = vbNormal
     Result = SetForegroundWindow(Me.hwnd)
     Me.Show
    Case WM_RBUTTONUP    '517 display popup menu
     Result = SetForegroundWindow(Me.hwnd)
'***** STOP! and make sure that your first menu item
' is named "mnu_1", otherwise you will get an erro below!!! *******
     Me.PopupMenu Me.mnu_1
  End Select
End Sub
Private Sub Form_Resize()
    'this is necessary to assure that the minimized window is hidden
    If Me.WindowState = vbMinimized Then Me.Hide
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'this removes the icon from the system tray
    Shell_NotifyIcon NIM_DELETE, nid
End Sub

Private Sub mPopExit_Click()
    'called when user clicks the popup menu Exit command
    Unload Me
End Sub

Private Sub mPopRestore_Click()
    'called when the user clicks the popup menu Restore command
    Me.WindowState = vbNormal
    Result = SetForegroundWindow(Me.hwnd)
    Me.Show
End Sub

Původní komentáře (3)
Obnoveno z Wayback Machine