[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

Systray won't refresh after removing icon

Systray won't refresh after removing icon
0
dgb
Asked:
dgb
  • 6
  • 5
  • 5
  • +3
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you please show us how you attempted to add/remove your tray icon?
0
 
dgbAuthor Commented:
Public Sub AddAppInTray(ByVal aHwnd As Long, _
                        ByVal anIconHandle As Long, _
                        ByVal aToolTipText As String)

   ' Set required properties
   mNotifyIconData.cbSize = Len(mNotifyIconData)
   mNotifyIconData.hwnd = aHwnd
   mNotifyIconData.uId = vbNull
   mNotifyIconData.uFlags = cNIF_ICON Or cNIF_TIP Or cNIF_MESSAGE
   mNotifyIconData.uCallBackMessage = cWM_MOUSEMOVE
   mNotifyIconData.hIcon = anIconHandle
   
   ' Chr$(0) is required at the end of the string
   mNotifyIconData.szTip = aToolTipText & vbNullChar
   
   ' Finally put the app in the tray...
   Shell_NotifyIcon cNIM_ADD, mNotifyIconData

   mAppInTray = True

End Sub


Public Sub RemoveAppFromTray()
   
   ' Only remove the app from the tray if it's in it...
   If mAppInTray Then
      Shell_NotifyIcon cNIM_DELETE, mNotifyIconData
   End If

End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This works for me...

Create a new project and add a PictureBox and a CommandButton.  The position and size of the PictureBox don't matter.  Set the Picture property of the PictureBox to the Icon you want to appear in the tray.

The app will drop to the tray on load, when it is minimized, or the close button [X] in the top right is clicked.  The COMPILED app can only be closed from the task manager.  The Button is simply there to allow a convenient way to close the app properly while in the IDE.

Option Explicit

Private Declare Sub Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA)

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 NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONDOWN = &H204

Private tic As NOTIFYICONDATA
Private inTray As Boolean

Private Sub Form_Load()
    Picture1.Visible = False
    Me.WindowState = vbMinimized
End Sub

Private Sub Form_Resize()
    If Me.WindowState = vbMinimized Then
        dropToTray
    End If
End Sub

Private Sub dropToTray()
    tic.cbSize = Len(tic)
    tic.hWnd = Picture1.hWnd ' Callback Control
    tic.uID = 1&
    tic.uFlags = NIF_DOALL
    tic.uCallbackMessage = WM_MOUSEMOVE
    tic.hIcon = Picture1.Picture
    tic.szTip = "My Application" & Chr$(0)
   
    Me.Visible = False
    Shell_NotifyIcon NIM_ADD, tic
    inTray = True
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    X = X / Screen.TwipsPerPixelX
    Select Case X
        Case WM_LBUTTONDBLCLK
            Shell_NotifyIcon NIM_DELETE, tic
            Me.WindowState = vbNormal
            Me.Visible = True
            Me.Show
            inTray = False
    End Select
End Sub

Private Sub Command1_Click()
    ' so you can close the app while in the IDE
    ' remove the button and this code for the finished app
    Unload Me
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case UnloadMode
        Case vbAppTaskManager, vbAppWindows, vbFormCode
            If inTray Then
                Shell_NotifyIcon NIM_DELETE, tic
            End If

        Case Else
            Cancel = True
            If Me.Visible = True Then
                dropToTray
            End If
           
    End Select
End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dgbAuthor Commented:
I do exactly what you've put in your example
(with my own names but the same idea)

I've looked around and it seems that more people have the same problem.
Program is gone icon is still there, when i place my mousepointer over it it disapears.

What i am looking fore is a way to refresh the systray
0
 
IThemaCommented:
This is a very interesting question. I don't know the answer myself, but experienced this behaviour a lot of times before with other (professional) programs, like the Norton virus scanner; the icon stays in the system tray, even though the program is killed/quit. Indeed, when you move your mouse over it, it disappears.
<<this post is just to get e-mail notifications about this question>>
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
When I remove the Icon on my system via code it automatically disappears on my system.

One difference I spotted was that I am assigning a uID:

    tic.uID = 1&

while you were not:

    mNotifyIconData.uId = vbNull

Can you build a NEW project with my directions and code and see if it reacts differently?
0
 
dgbAuthor Commented:
I've changed vbNull into 1& but nothing changed.
0
 
dgbAuthor Commented:
I have to go but i will be back first thing tomorrow
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Please try a NEW project with my code and directions and let us know how it behaves...
0
 
fostejoCommented:
dgb (&IThema),

You'll generally see this type of behaviour when the application unexpectedly closes before it gets chance to correctly remove the icon from the Notification area (tray)

I'd suggest (as Idle_Mind does) either using his code or, alternatively, stepping through your own code using VB's 'execute next line' (F8 in VB6) to ensure that its doing what you expect it to do at each individual line.

cheers,
0
 
Mark_FreeSoftwareCommented:

are you sure you are executing the code to remove the icon from system tray?
because when your app unloads before removing the icon from the tray it will stay there


to try: place the code to unload in a commandbutton and then press it to remove from system tray
0
 
Leo EikelmanCommented:
Look at this example

http://www.vbbrasil.com/download/traynot.exe

Hope this helps,

Leo
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Leo, how does a weight loss supplement help?!    Lol...
0
 
Leo EikelmanCommented:
..... no comment.

and I lost the original link I had for you. Wooptie Doo!

: /
0
 
dgbAuthor Commented:
I have changed the location of "RemoveAppFromTray".

it is now the first thing i do when the user stops the program.

I will use the rest of the day to check it this is the answer.

After that i will devide the points (any suggestions)
0
 
Leo EikelmanCommented:
I don't have too much experience with this but these examples might help

Example 1

in a form :
'     'on the form1

Private Sub GoSystemTray()

     VBGTray.cbSize = Len(VBGTray)
     VBGTray.hwnd = Me.hwnd
     VBGTray.uId = vbNull
     VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
     VBGTray.ucallbackMessage = WM_MOUSEMOVE
     
     VBGTray.hIcon = Me.Icon
    '     'tooltiptext
     VBGTray.szTip = Me.Caption & vbNullChar
     Call Shell_NotifyIcon(NIM_ADD, VBGTray)
     App.TaskVisible = False 'remove application from taskbar
     Me.Hide
End Sub


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

     Static lngMsg As Long
     Static blnFlag As Boolean
     Dim result As Long
     
    lngMsg = X / Screen.TwipsPerPixelX

     If blnFlag = False Then
        blnFlag = True
        Select Case lngMsg
        '     'doubleclick
        Case WM_LBUTTONDBLCLICK
         Me.Show
        '     'right-click
        Case WM_RBUTTONUP
         result = SetForegroundWindow(Me.hwnd)
         Me.PopupMenu mnuSystemtray
    End Select

        blnFlag = False
     End If

End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

     VBGTray.cbSize = Len(VBGTray)
     VBGTray.hwnd = Me.hwnd
     VBGTray.uId = vbNull
     Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub

in a module:

Public Const WM_LBUTTONDBLCLICK = &H203
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

Public 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

    Public VBGTray As NOTIFYICONDATA

Declare Function Shell_NotifyIcon Lib "shell32" Alias _
    "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long




Example 2


Public Const WM_LBUTTONDBLCLICK = &H203
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

Public 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

Public VBGTray As NOTIFYICONDATA

Declare Function Shell_NotifyIcon Lib "shell32" Alias _
       "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

'****************************************************************
' Name: Windows 95 System Tray
' Description:Place your app's icon in the windows 95 system
'      tray!
'     'thanxs to arno pijnappels
'     'modifications also by Rich Jones <rich_jones@wmg.com>
' By: Found on the World Wide Web
'
' Inputs:None
' Returns:None
' Assumes:None
' Side Effects:None
'
'Code provided by Planet Source Code(tm) 'as is', without
'     warranties as to performance, fitness, merchantability,
'     and any other warranty (whether expressed or implied).
'****************************************************************

'     'on the form1

Private Sub GoSystemTray()

        VBGTray.cbSize = Len(VBGTray)
        VBGTray.hWnd = Me.hWnd
        VBGTray.uId = vbNull
        VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        VBGTray.ucallbackMessage = WM_MOUSEMOVE
       
        VBGTray.hIcon = Me.Icon
       '     'tooltiptext
        VBGTray.szTip = Me.Caption & vbNullChar
        Call Shell_NotifyIcon(NIM_ADD, VBGTray)
        App.TaskVisible = False'remove application from taskbar
        Me.Hide
End Sub


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

        Static lngMsg As Long
        Static blnFlag As Boolean
        Dim result As Long
       
       lngMsg = X / Screen.TwipsPerPixelX
        If blnFlag = False Then
       blnFlag = True
       Select Case lngMsg
       '     'doubleclick
       Case WM_LBUTTONDBLCLICK
        Me.Show
       '     'right-click
       Case WM_RBUTTONUP
        result = SetForegroundWindow(Me.hwnd)
        Me.PopupMenu mnuSystemtray
End Select

blnFlag = False
 End If
End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

        VBGTray.cbSize = Len(VBGTray)
        VBGTray.hWnd = Me.hWnd
        VBGTray.uId = vbNull
        Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub



Maybe somewhere in there you can find your solution


Hope that helps,

Leo
0
 
dgbAuthor Commented:
Leo:
Thanks but i hope it is already solved
(see comment just above yours)
0
 
Leo EikelmanCommented:
Yeah I saw that.  Just incase it doesn't work you can try to extract some code from above.

Happy coding : )

Leo
0
 
Leo EikelmanCommented:
split

Leo
0
 
IThemaCommented:
Hi Dan,

Quote dgb:
>I will use the rest of the day to check it this is the answer.
>After that i will devide the points (any suggestions)

Another quote:
>Thanks but i hope it is already solved

Points however, have never been split in the end...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now