Results 1 to 15 of 15

Thread: [RESOLVED] System Tray Menu

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Resolved [RESOLVED] System Tray Menu

    Well here is my third and hopefully last thread of the night.

    I have searched for the last hour or so on google and here on the forum for a way to add an icon to the system tray that works as a menu. I have been unsuccessful. I'm looking for a nice easy way to do this. If anyone can help me it would be great. Thanks!!

  2. #2

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    I'm using the code that static suggested. But how do I give it a menu now. I know how to have a drop down menu at the top of the form. How do I do it for the tray icon though? Thanks.

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: System Tray Menu

    do you know how to make a popup menu?
    basically create a menu with the top level visible = false
    then call popupmenu "MenuName" from the mousedown event of the form (I think it was the form)

    do a search here.. many examples are kickin around. Systray Popup menu
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    Yeah I can do a popup menu.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: System Tray Menu

    In the Form_Mousedown event you would detect if the Button = 2 (right click) and if so then just popup your menu.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    How do I get it to pop up if I right click on the icon on the tray though?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: System Tray Menu

    As mentioned...
    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 2 Then
            Me.PopupMenu "MyMenu"
        End If
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: System Tray Menu

    Which event you should write the code that shows the popup menu depends on what you've set the hWnd and uCallbackMessage members of the NOTIFYICONDATA structure to. For example, if you've set the hWnd member to the hWnd of your main Form and the uCallbackMessage to WM_MOUSEMOVE (which is the message I would recommend you to use). In that case the Form_MouseMove event will be fired everytime an event has happened on the icon. Which event that is (WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_RBUTTONDOWN and so on) will then be stored in the X parameter of the event. If the ScaleMode of your Form is set to Twips you need to divide X with Screen.TwipsPerPixelX to get the correct value. So the code should look simular to this:
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim nMsg As Long
        
        nMsg = X \ Screen.TwipsPerPixelX
        Select Case nMsg
            Case WM_RBUTTONDOWN
                'the right mouse button was pressed show the popup menu
                Me.PopupMenu mnuPop
        End Select
    End Sub

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    Quote Originally Posted by RobDog888
    As mentioned...
    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 2 Then
            Me.PopupMenu "MyMenu"
        End If
    End Sub
    That only works if I right click on the form? It does nothing at all if I right click on the icon.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: System Tray Menu

    Like Joacim posted, it depends on what you set the callback.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    Callback?

    Yeah i'm still a beginner at all this... Could you explain in more detail what i have to do please? This is the code that I am using:

    Code:
    Option Explicit
    
        Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long
        
        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 Const NIM_ADD = &H0
        Private Const NIM_MODIFY = &H1
        Private Const NIM_DELETE = &H2
        Private Const NIF_MESSAGE = &H1
        Private Const NIF_ICON = &H2
        Private Const NIF_TIP = &H4
        
        Private Const WM_LBUTTONDBLCLK = &H203
        Private Const WM_LBUTTONDOWN = &H201
        Private Const WM_RBUTTONUP = &H205
        
        Dim NID As NOTIFYICONDATA
    
    
    Private Sub Form_Load()
    Dim str As String
    
            With NID
                .cbSize = Len(NID)
                .hIcon = Me.Icon
                .hWnd = Me.hWnd
                .szTip = "Hello" & vbNullChar
                .ucallbackMessage = WM_LBUTTONDBLCLK
                .uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
                .uId = 1&
            End With
            
            Shell_NotifyIcon NIM_ADD, NID
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
    Form1.PopupMenu menumain
    End If
    End Sub
    Thanks in advance.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: System Tray Menu

    Change the .ucallbackMessage from ".ucallbackMessage = WM_LBUTTONDBLCLK" to ".ucallbackMessage = WM_MOUSEMOVE".
    Then use the code Joacim posted.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: System Tray Menu

    It works!!! Thanks so much to both of you!!!!

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