Change Application Icon on the fly

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Here a little procedure to change the application icon anytime after the start.

Code: Select all

; (c) 2001 - Franco's template - absolutely freeware
; How to change an application icon on the fly
 
Procedure ChangeAppIcon(Image$)
  hIco=LoadImage(0,Image$)
  SetClassLong_(WindowID(),#GCL_HICON,hIco)
EndProcedure
 
;test
If OpenWindow(0, 200, 200, 320,240, #PB_Window_SystemMenu ,"Test Window")
ChangeAppIcon(".\myicon.ico") ; <- your icon name...
 
  Repeat
    EventID.l = WaitWindowEvent()
  Until EventID = #PB_EventCloseWindow
EndIf
 
End
Here the thread version:

You can also make an animated icon (you need 32 different icons)
PureBasic is fast enough for this!
BUT you have to put a DELAY(x) command in the procedure!!!
Otherwise nasm will KILL your OS!!! (tested on Win98)

Code: Select all

; (c) 2001 - Franco's template - absolutely freeware
; how to make an animated application icon
 
Procedure AnimatedAppIcon(Image$)
  Icon=1 ; <- icon name must be something like icon_1.ico to icon_32.ico
  Repeat
    hIco=LoadImage(0,Image$+Str(Icon)+".ico")
    SetClassLong_(WindowID(),#GCL_HICON,hIco)
    Icon=Icon+1 : If Icon=32 : Icon=1 : EndIf
    Delay(50) ;<- don't delete this, otherwise nasm will crash!
  ForEver
EndProcedure
 
;test
If OpenWindow(0, 200, 200, 320,240, #PB_Window_SystemMenu ,"Test Window")
 
CreateThread(@AnimatedAppIcon(),".\icon_") ; <- without extension!
 
   Repeat
    EventID.l = WaitWindowEvent()
  Until EventID = #PB_EventCloseWindow
EndIf
I suppose it must be possible to work with the IncludeBinary command ... to play with.



Have a nice day...
Franco