EnableExplicit

LibBuild_CheckPriority(LoadResEx_ImageBitmapWEMF_MultiOS)
LibBuild_CheckPriority(LoadResEx_ImageIconWEMF_MultiOS)

Procedure.i LoadResEx_Image_MultiOS(ResID.i, ResType.i, SizeX.i = 0, SizeY.i = 0)
  Define.i xResult
  Define.i xFlags
  Define.i xLong1
  
  If ResType = 0
    xFlags = $2040
    SizeX = 0
    SizeY = 0
    ElseIf SizeX = 0 And SizeY = 0
    SizeX = 32
    SizeY = 32
    ElseIf SizeX = 0
    SizeX = SizeY
    ElseIf SizeY = 0
    SizeY = SizeX
    EndIf

  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    If IsMinWindowsVista
      xResult = LoadImage_(hInstance, ResID + 1, ResType, SizeX, SizeY, xFlags)
      EndIf
    If Not xResult
      xResult = LoadImage_(hInstance, ResID, ResType, SizeX, SizeY, xFlags)
      EndIf
    CompilerElseIf #PB_Compiler_Unicode
    If IsHighColor
      If IsMinWindowsVista
        xLong1 = 5
        ElseIf IsMinWindowsXP
        xLong1 = 4
        ElseIf IsMinWindows2000
        xLong1 = 3
        Else
        xLong1 = 2
        EndIf
      While xLong1 > 1 And Not xResult
        xResult = LoadImage_(hInstance, ResID + xLong1, ResType, SizeX, SizeY, xFlags)
        xLong1 - 1
        Wend
      EndIf
    If Not xResult
      If IsMinWindows2000
        xResult = LoadImage_(hInstance, ResID + 1, ResType, SizeX, SizeY, xFlags)
        EndIf
      If Not xResult
        xResult = LoadImage_(hInstance, ResID, ResType, SizeX, SizeY, xFlags)
        EndIf
      EndIf
    CompilerElse
    If IsHighColor
      If IsMinWindowsME
        xLong1 = 4
        ElseIf IsMinWindows98
        xLong1 = 3
        Else
        xLong1 = 2
        EndIf
      While xLong1 > 1 And Not xResult
        xResult = LoadImage_(hInstance, ResID + xLong1, ResType, SizeX, SizeY, xFlags)
        xLong1 - 1
        Wend
      EndIf
    If Not xResult
      If IsMinWindowsME
        xResult = LoadImage_(hInstance, ResID + 1, ResType, SizeX, SizeY, xFlags)
        EndIf
      If Not xResult
        xResult = LoadImage_(hInstance, ResID, ResType, SizeX, SizeY, xFlags)
        EndIf
      EndIf
    CompilerEndIf
  ProcedureReturn xResult
  EndProcedure

; IDE Options = PureBasic 5.21 LTS (Windows - x86)
; CursorPosition = 3
; Folding = -
; EnableUnicode
; EnableXP