[Windows] Get modern system icons

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

[Windows] Get modern system icons

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: [Windows] Get modern system icons

Post by jacdelad »

Thanks Little John, I use the "normal" API regularly sand never came across the other one. That's quite interesting!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: [Windows] Get modern system icons

Post by ChrisR »

I hadn't seen this API either.
Thanks for sharing it with us, it can be useful.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [Windows] Get modern system icons

Post by Little John »

You are both welcome! I'm glad that the code is useful for other people, too.
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: [Windows] Get modern system icons

Post by Kuron »

Thank you, this is incredibly helpful! You have just helped everybody make their projects look much better!
Best wishes to the PB community. Thank you for the memories. ♥️
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: [Windows] Get modern system icons

Post by BarryG »

Thanks. I still prefer the old look instead of the boring flat new look, though.
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: [Windows] Get modern system icons

Post by ozzie »

Thanks. I've been looking for various ways to improve the UI and this will help.
Post Reply