[INCLUDE]
LoadResEx_Icon_Shared

[CODE]
#If Shell_NotifyIcon Then
Public Sub LoadResEx_WindowIcon(ByVal hwnd As Long, ByVal ResID As Long, Optional ByVal ptrTrayToUpdate As Long)
#Else
Public Sub LoadResEx_WindowIcon(ByVal hwnd As Long, ByVal ResID As Long)
#End If
  Dim Old1 As Long
  Dim Old2 As Long
  #If Shell_NotifyIcon Then
    Dim Old3 As Long
    Dim xType1 As NOTIFYICONDATA
    Dim xLong4 As Long
    #End If
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  '4th!
  
  Old1 = SendMessage(hwnd, &H80&, 0&, 0&)
  Old2 = SendMessage(hwnd, &H80&, 1&, 0&)
  #If Shell_NotifyIcon Then
    If Not ptrTrayToUpdate = 0& Then
      Call RtlMoveMemory(xType1, ByVal ptrTrayToUpdate, 24&)
      Old3 = xType1.hIcon
      End If
    #End If
  If Not ResID = -1& Then
    For xLong1 = LoadResex_Caps_WinDIcon To 0& Step -1&
      If IconIDIsUseable(xLong1, LoadResex_Caps_WinDIcon) Then
        #If InIDE And LoadResEx_LanguageFile Then
          xLong2 = LoadImageA(LoadResEx_LanguageFile_hModul, ByVal (ResID + xLong1), 1&, 16&, 16&, 0&)
          xLong3 = LoadImageA(LoadResEx_LanguageFile_hModul, ByVal (ResID + xLong1 + 15), 1&, 32&, 32&, 0&)
          #Else
          xLong2 = LoadImageA(App.hInstance, ByVal (ResID + xLong1), 1&, 16&, 16&, 0&)
          xLong3 = LoadImageA(App.hInstance, ByVal (ResID + xLong1 + 15), 1&, 32&, 32&, 0&)
          #End If
        If xLong2 = 0& Then
          If Not xLong3 = 0& Then xLong2 = xLong3: GoTo UpdateMain
          Else
          If xLong3 = 0& Then xLong3 = xLong2
UpdateMain:
          Call SendMessage(hwnd, &H80&, 0&, xLong2)
          Call SendMessage(hwnd, &H80&, 1&, xLong3)
          
'Stop
          'If Not (Old1 = 0&) Then Call DestroyIcon(Old1)
          'If Not (Old2 = Old1 Or Old2 = 0&) Then Call DestroyIcon(Old2)
          
          #If Shell_NotifyIcon Then
            If Not ptrTrayToUpdate = 0& Then
              If IconIDIsUseable(xLong1, LoadResex_Caps_TrayIcon) Then
                GoTo RealUpdateTrayPre
                Else
                For xLong3 = xLong1 - 1& To 0& Step -1&
                  If IconIDIsUseable(xLong3, LoadResex_Caps_TrayIcon) Then
                    #If InIDE Then
                      xLong4 = LoadImageA(LoadResEx_LanguageFile_hModul, ByVal (ResID + xLong3), 1&, 16&, 16&, 0&)
                      #Else
                      xLong4 = LoadImageA(App.hInstance, ByVal (ResID + xLong3), 1&, 16&, 16&, 0&)
                      #End If
                    If Not xLong4 = 0& Then GoTo RealUpdateTray
                    End If
                  Next
RealUpdateTrayPre:
                xLong4 = xLong2
RealUpdateTray:
                xType1.hIcon = xLong4
                Call RtlMoveMemory(ByVal ptrTrayToUpdate, xType1, 24&)
                If Not xType1.Reserved_cbsize = 0& Then Call Shell_NotifyIconEx(1&, ptrTrayToUpdate)
                End If
              End If
            'AndClearTray
            #End If
          Exit For
          End If
        End If
      Next
    End If
  
  If Not (Old1 = 0&) Then Call DestroyIcon(Old1)
  If Not (Old2 = Old1 Or Old2 = 0&) Then Call DestroyIcon(Old2)
  #If Shell_NotifyIcon Then
    'AndClearTray:
    If Not (Old3 = Old1 Or Old3 = 0&) Then Call DestroyIcon(Old3)
    #End If
  End Sub
