Page 1 of 1

[Windows] Get modern system icons

Posted: Tue Nov 14, 2023 11:59 am
by Little John
LoadIcon_() and LoadImage_() retrive the old fashioned Windows system icons.
In order to get the modern icons, use LoadIconWithScaleDown_().

Code: Select all

; tested with PB 6.03 LTS (x64) on Windows 11

EnableExplicit

; ------------------------------------------------------------------
; -- Get *modern* system icons;
;    Windows Vista or newer required
; <https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-loadiconwithscaledown>
Prototype.i ProtoLoadIconWithScaleDown (hInst.i, *szName, cx.i, cy.i, *hIco)
Global LoadIconWithScaleDown_.ProtoLoadIconWithScaleDown
Define dll.i = OpenLibrary(#PB_Any, "Comctl32.dll")
If dll
   LoadIconWithScaleDown_ = GetFunction(dll, "LoadIconWithScaleDown")
   CloseLibrary(dll)
EndIf
; ------------------------------------------------------------------

Enumeration
   #WinMain
EndEnumeration


Procedure.i ShowSystemIcons (iconSize.i)
   Protected.i hImg, hIco
   
   If OpenWindow(#WinMain, 0, 0, 150+iconSize, 4*10+3*iconSize, "Windows system icons", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) = 0
      MessageRequester("Error",
                       "Can't open window.",
                       #PB_MessageRequester_Error)
      ProcedureReturn 0
   EndIf
   
   ; old fashioned
   ImageGadget(#PB_Any, 20, 10, iconSize, iconSize, LoadIcon_(#Null, #IDI_INFORMATION))
   
   ; old fashioned
   hImg = LoadImage_(#Null, #IDI_INFORMATION, #IMAGE_ICON, iconSize, iconSize, #LR_SHARED)
   ImageGadget(#PB_Any, 20, 2*10+iconSize, iconSize, iconSize, hImg)
   
   ; modern style
   If LoadIconWithScaleDown_ <> 0 And LoadIconWithScaleDown_(#Null, #IDI_INFORMATION, DesktopScaledX(iconSize), DesktopScaledY(iconSize), @hIco) = #S_OK
      ImageGadget(#PB_Any, 20, 3*10+2*iconSize, iconSize, iconSize, hIco)
   EndIf
   
   ProcedureReturn 1
EndProcedure


ShowSystemIcons(32)
; ShowSystemIcons(64)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: [Windows] Get modern system icons

Posted: Tue Nov 14, 2023 1:50 pm
by jacdelad
Thanks Little John, I use the "normal" API regularly sand never came across the other one. That's quite interesting!

Re: [Windows] Get modern system icons

Posted: Tue Nov 14, 2023 3:18 pm
by ChrisR
I hadn't seen this API either.
Thanks for sharing it with us, it can be useful.

Re: [Windows] Get modern system icons

Posted: Tue Nov 14, 2023 5:04 pm
by Little John
You are both welcome! I'm glad that the code is useful for other people, too.

Re: [Windows] Get modern system icons

Posted: Tue Nov 14, 2023 9:03 pm
by Kuron
Thank you, this is incredibly helpful! You have just helped everybody make their projects look much better!

Re: [Windows] Get modern system icons

Posted: Wed Nov 15, 2023 2:54 am
by BarryG
Thanks. I still prefer the old look instead of the boring flat new look, though.

Re: [Windows] Get modern system icons

Posted: Wed Nov 15, 2023 3:40 am
by ozzie
Thanks. I've been looking for various ways to improve the UI and this will help.