[INCLUDE]
LoadResEx_Icon_Shared

[CODE]
Public Function LoadResEx_WindowIcon(ByVal hwnd As Long, ByVal IconResID As Long, Optional ByVal ResolveTrayIconAndKeepAlive As Boolean = False) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
    
  If Not (hwnd = 0& And Not ResolveTrayIconAndKeepAlive) Then
    If hwnd = 0& Then
      xLong2 = LoadResex_Caps_TrayIcon
      Else
      xLong2 = LoadResex_Caps_WinDIcon
      End If
  
    For xLong1 = xLong2 To 0& Step -1&
      If IconIDIsUseable(xLong1, xLong2) Then
        xLong3 = LoadImageA(App.hInstance, "#" & CStr(IconResID + xLong1), 1&, 16&, 16&, &H8000&)
        'xLong3 = LoadImageA(LoadResEx_LanguageFile_hModul, "#" & CStr(IconResID + xLong1), 1&, 16&, 16&, &H8000&)
        If Not xLong3 = 0& Then
          If hwnd = 0& Then
            LoadResEx_WindowIcon = xLong3
            Exit For
            Else
            Call SendMessage(hwnd, &H80&, 0&, xLong3)
            xLong4 = LoadImageA(App.hInstance, "#" & CStr(IconResID + xLong1 + 15), 1&, 32&, 32&, &H8000&)
            If Not xLong4 = 0& Then
              Call SendMessage(hwnd, &H80&, 1&, xLong4)
              Call DestroyIcon(xLong4)
              End If

            If ResolveTrayIconAndKeepAlive Then
              If IconIDIsUseable(xLong1, LoadResex_Caps_TrayIcon) Then
                LoadResEx_WindowIcon = xLong3
                Exit For
                Else
                Call DestroyIcon(xLong3)
                xLong2 = LoadResex_Caps_TrayIcon
                hwnd = 0&
                End If
              Else
              Call DestroyIcon(xLong3)
              Exit For
              End If
            End If
          End If
        End If
      Next
    End If
  End Function
