Results 1 to 2 of 2

Thread: Pop up menu from system tray

  1. #1
    Guest

    Post

    I need a code example to popup a menu from an Icon I set into the system tray.
    when I try to pop up a menu using 3rd party tool as the Sheridan toolbar I get an error that the form is not visible.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Add an Invisible Picturebox to a Form, set the Form's ShowInTaskbar Property to False, add a Menu called mnuPopup with SubItems called, mnuShow and mnuExit..
    Code:
    Private 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
    
    Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
    
    Private Const NIF_ICON = &H2
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_TIP = &H4
    Private Const NIM_ADD = &H0
    Private Const NIM_DELETE = &H2
    Private Const WM_MOUSEMOVE = &H200
    Private Const WM_RBUTTONUP = &H205
    Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    
    Private tTrayIcon As NOTIFYICONDATA
    
    Private Sub Form_Load()
        With tTrayIcon
            .hIcon = Icon
            .hwnd = Picture1.hwnd
            .szTip = Caption & Chr(0)
            .uCallbackMessage = WM_MOUSEMOVE
            .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
            .uID = 1
            .cbSize = Len(tTrayIcon)
        End With
        Shell_NotifyIcon NIM_ADD, tTrayIcon
    End Sub
    
    Private Sub Form_Resize()
        If WindowState = vbMinimized Then
            Hide
            WindowState = vbNormal
        End If
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Shell_NotifyIcon NIM_DELETE, tTrayIcon
    End Sub
    
    Private Sub mnuExit_Click()
        Unload Me
    End Sub
    
    Private Sub mnuShow_Click()
        Show
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Select Case ScaleX(X, vbTwips, vbPixels)
        Case WM_LBUTTONDBLCLK
            'Double Click on SysTray Icon to Show Form
            Show
        Case WM_RBUTTONUP
            'Used for Displaying a Popup Menu
            Me.PopupMenu mnuPopup
        End Select
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

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