How to refresh system tray?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to refresh system tray?

  1. #1
    Jacky Guest

    How to refresh system tray?

    Hello,
    I terminated an app in system tray by code.But I found the app's icon
    was still in the tray after I terminated it,though it disappeared when I
    moved the mouse pointer over it.So I wonder how to refresh the system tray
    after I terminated apps in it?
    Thanks.

    Jacky




  2. #2
    Jonathan Wood Guest

    Re: How to refresh system tray?

    I don't know that there's any way to do this programatically.

    You can refresh the tray but that just repaints it and that won't make the
    icon go away. Windows doesn't realize the app is still there until you move
    the mouse over it and Windows can't do a callback.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    Jacky <jackyyin@netease.com> wrote in message news:39367426@news.devx.com...
    > Hello,
    > I terminated an app in system tray by code.But I found the app's icon
    > was still in the tray after I terminated it,though it disappeared when I
    > moved the mouse pointer over it.So I wonder how to refresh the system tray
    > after I terminated apps in it?
    > Thanks.
    >
    > Jacky
    >
    >
    >




  3. #3
    Jonathan Wood Guest

    Re: How to refresh system tray?

    I don't know that there's any way to do this programatically.

    You can refresh the tray but that just repaints it and that won't make the
    icon go away. Windows doesn't realize the app is still there until you move
    the mouse over it and Windows can't do a callback.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    Jacky <jackyyin@netease.com> wrote in message news:39367426@news.devx.com...
    > Hello,
    > I terminated an app in system tray by code.But I found the app's icon
    > was still in the tray after I terminated it,though it disappeared when I
    > moved the mouse pointer over it.So I wonder how to refresh the system tray
    > after I terminated apps in it?
    > Thanks.
    >
    > Jacky
    >
    >
    >




  4. #4
    Karl E. Peterson Guest

    Re: How to refresh system tray?

    Hi Jonathan / Jacky --

    > > I terminated an app in system tray by code.But I found the app's icon
    > > was still in the tray after I terminated it,though it disappeared when I
    > > moved the mouse pointer over it.So I wonder how to refresh the system tray
    > > after I terminated apps in it?

    >
    > I don't know that there's any way to do this programatically.
    >
    > You can refresh the tray but that just repaints it and that won't make the
    > icon go away. Windows doesn't realize the app is still there until you move
    > the mouse over it and Windows can't do a callback.


    mouse_event? Do a GetWindowRect on the tray, grab the current mouse coords, move it
    through the tray, and put it back where it was? If it sounds worth a shot, Jacky,
    you can grab a sample (MouseEvent.zip) from my site.

    Later... Karl
    --
    http://www.mvps.org/vb




  5. #5
    Karl E. Peterson Guest

    Re: How to refresh system tray?

    Hi Jonathan / Jacky --

    > > I terminated an app in system tray by code.But I found the app's icon
    > > was still in the tray after I terminated it,though it disappeared when I
    > > moved the mouse pointer over it.So I wonder how to refresh the system tray
    > > after I terminated apps in it?

    >
    > I don't know that there's any way to do this programatically.
    >
    > You can refresh the tray but that just repaints it and that won't make the
    > icon go away. Windows doesn't realize the app is still there until you move
    > the mouse over it and Windows can't do a callback.


    mouse_event? Do a GetWindowRect on the tray, grab the current mouse coords, move it
    through the tray, and put it back where it was? If it sounds worth a shot, Jacky,
    you can grab a sample (MouseEvent.zip) from my site.

    Later... Karl
    --
    http://www.mvps.org/vb




  6. #6
    Join Date
    Jan 2008
    Posts
    1
    When you setup your tray icon, you identify a message to be sent to your window handler for all try icon notifications. If you are using a case structure to handle some of these events, like WM_LBUTTONUP or WM_RBUTTONDOWN, be sure you have a "default" case that calls DefWindowProc for this message. If you don't call DefWindowProc for every tray-icon notify event that you don't handle yourself, then Windows will miss some events, such as the ones that clean up the tray. I saw the same condition you report and it went away after checking and fixing this. Good luck!

    -Artemis99

  7. #7
    Join Date
    Jan 2010
    Posts
    1

    I know this is late, but your post was helpful to me in a way.

    I've figured out how to remove the icon using the Shell_NotifyIcon API.

    What you have to do is grab the window hWnd when you instantiate the variable Notification Icon Data. Put it into a global variable in one of your modules. When you unload the form, you simply instantiate another Notify Icon Variable like this...

    With mynid
    .cbSize = Len(mynid)
    .hwnd = curhwnd
    .uId = vbNull
    .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    .uCallBackMessage = WM_MOUSEMOVE
    .hIcon = curicon
    .szTip = "Data For Display" & vbNullChar
    End With

    where curhwnd is the global variable you created when the form was opened.
    I also created a global variable for the current icon "curicon" just in case. I don't know if the Me.Icon would work when the form is closing.

    The reason I had to do all of this is that for some reason the Notify Icon Data variable that I created as a private variable kept having its data rewritten and therefore when I called Shell_NotifyIcon NIM_DELETE, nid it wouldn't know what hWnd I was talking about because the nid.hWnd variable was set to 0. When I created mynid variable and tried to substitute the .hWnd value with Me.hWnd it would return a different hWnd from when the form was first initialized so this too wouldn't remove the icon in the tray. That's when I realized I would have to capture the hWnd when the nid was first created and before the Shell_NotifyIcon NIM_CREATE,nid was called. When I did this and created mynid with the .hWnd variable set to curhwnd from my global module, the icon was successfully removed from the tray.

    Included Bellow is a step by step guide....

    RESOURCE PAGE
    http://support.microsoft.com/kb/176085


    '######################################################################################### #############################################
    '# THIS SECTION NEEDS TO BE PLACE IN A GLOBAL MODULE
    '######################################################################################### #############################################
    Global curhwnd As String
    Global curicon As String
    '######################################################################################### #############################################
    '# THIS SECTION NEEDS TO BE PLACE IN A GLOBAL MODULE
    '######################################################################################### #############################################




    '######################################################################################### #############################################
    '# THIS SECTION OF CODE NEEDS TO BE INSERTED IN THE DISPLAY SECTION OF THE FORM
    '######################################################################################### #############################################\
    curhwnd = Me.hwnd
    curicon = Me.Icon
    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 = "Display Text For Icon" & vbNullChar
    End With
    Shell_NotifyIcon NIM_ADD, nid
    '######################################################################################### #############################################
    '# THIS SECTION OF CODE NEEDS TO BE INSERTED IN THE DISPLAY SECTION OF THE FORM
    '######################################################################################### #############################################


    '######################################################################################### #############################################
    '# BELOW IS THE THE CONSTANTS AND VARIABLES THAT NEED TO BE ADDED TO THE TOP OF THE FORM (CODE)
    '######################################################################################### #############################################
    ' Constants for managing System Tray tasks, foudn in shellapi.h
    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

    'constants required by Shell_NotifyIcon API call:
    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_MOUSEMOVE = &H200
    Private Const WM_LBUTTONDOWN = &H201 'Button down
    Private Const WM_LBUTTONUP = &H202 'Button up
    Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
    Private Const WM_RBUTTONDOWN = &H204 'Button down
    Private Const WM_RBUTTONUP = &H205 'Button up
    Private Const WM_RBUTTONDBLCLK = &H206 'Double-click

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

    Private nid As NOTIFYICONDATA
    '######################################################################################### #############################################
    '# ABOVE IS THE THE CONSTANTS AND VARIABLES THAT NEED TO BE ADDED TO THE TOP OF THE FORM (CODE)
    '######################################################################################### #############################################


    '######################################################################################### #############################################
    '# THESE SUBROUTINES NEED TO BE PLACED IN YOUR CODE ANYWHERE YOU LIKE
    '######################################################################################### #############################################
    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
    '##############################################
    '# YOU HAVE TO PLAY AROUND WITH THIS SECTION A BIT TO GET THE
    '# RIGHT ACTION TO HAPPEN.
    '# MY SUGGESTION IS TO USE MSGBOX MSG TO SEE WHAT NUMBER
    '# COMES BACK WHEN YOU CLICK A BUTTON OR MOVE THE MOUSE.
    '##############################################
    'Case WM_LBUTTONUP '514 restore form window
    'Me.WindowState = vbNormal
    'Result = SetForegroundWindow(Me.hwnd)
    'Me.Show
    'Exit Sub
    Case 0 To 515
    Exit Sub
    Case 515 'Mouse over icon
    Exit Sub
    Case 515 To 518
    Exit Sub
    Case 519 To 520 'Right mouse click... for some reason is 519 and 520
    Result = SetForegroundWindow(Me.hwnd)
    'Me.PopupMenu Me.mPopupSys
    Exit Sub
    End Select
    'MsgBox Msg
    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)
    Dim mynid As NOTIFYICONDATA
    With mynid
    .cbSize = Len(mynid)
    .hwnd = curhwnd
    .uId = vbNull
    .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    .uCallBackMessage = WM_MOUSEMOVE
    .hIcon = curicon
    .szTip = "Documentum Table Manager" & vbNullChar
    End With
    Shell_NotifyIcon NIM_DELETE, mynid
    End Sub
    '######################################################################################### #############################################
    '# THESE SUBROUTINES NEED TO BE PLACED IN YOUR CODE ANYWHERE YOU LIKE
    '######################################################################################### #############################################

  8. #8
    Join Date
    Apr 2010
    Posts
    1
    Hello,

    And what about terminated an EXTERN app in system tray by code. The app's icon still in the tray after terminated it, and, like Jacky, it disappeared when I
    moved the mouse pointer over it. So, I whish how to refresh the system tray
    after I terminated an extern apps in it ?
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center