Changing Taskbar icon in realtime

Posted on 2006-06-08
Last Modified: 2010-04-07
Im trying to find a way to change the icon in realtime i have the icons in a imagelist. I tried changing NIM_ADD to NIM_MODIFY thinking that might be it but it didnt work...

Private Sub LoadSysTrayIcon()
       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 = AppName & "  " & "Version: " & AppVer
       End With
       Shell_NotifyIcon NIM_ADD, nid
End Sub

'===========================MODULE CODE
      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

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

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

      Public nid As NOTIFYICONDATA
Question by:thaburner
    LVL 85

    Accepted Solution

    That's correct.

    Here is an example that toggles the Icon in the tray between the first and second images in the ImageList:

    Option Explicit

    Private Declare Sub Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As 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_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 toggle As Boolean

    Private Sub Form_Load()
        Timer1.Enabled = False
        Timer1.Interval = 1000
        Picture1.Visible = False
        Picture2.Visible = False
        inTray = False
        Me.WindowState = vbMinimized
    End Sub

    Private Sub Form_Resize()
        If Me.WindowState = vbMinimized Then
        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 = ImageList1.ListImages(1).Picture
        tic.szTip = "My Application" & Chr$(0)
        Me.Visible = False
        Shell_NotifyIcon NIM_ADD, tic
        inTray = True
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Timer()
        If inTray Then
            toggle = Not toggle
            If toggle Then
                tic.hIcon = ImageList1.ListImages(1).Picture
                tic.hIcon = ImageList1.ListImages(2).Picture
            End If
            Shell_NotifyIcon NIM_MODIFY, tic
        End If
    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
                Timer1.Enabled = False
                inTray = False
                Shell_NotifyIcon NIM_DELETE, tic
                Me.WindowState = vbNormal
                Me.Visible = True
        End Select
    End Sub

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

            Case Else
                Cancel = True
                If Me.Visible = True Then
                End If
        End Select
    End Sub

    Author Comment

    that did the trick thanks again Idle

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now