Set a Window-Icon (Only Windows!!!)

Previous topic - Next topic

Schranz0r

This code load a new WindowIcon, without a resourcehacker!  

Sorry but windows ONLY!


Code (glbasic) Select
SetIcon("icon.ico")

WHILE TRUE

PRINT "look at the window-icon!!",10,10

SHOWSCREEN
WEND
END


FUNCTION Close:
ENDFUNCTION

INLINE
DECLARE_C_ALIAS(u32_GetActiveWindow,"user32.dll", "GetActiveWindow",(),int);
DECLARE_C_ALIAS(u32_SetClassLong,"user32.dll", "SetClassLongA",(int,int,int), void);
DECLARE_C_ALIAS(shell32_LoadIcon,"Shell32.dll", "ExtractIconA",(int,const char*,int), int);
ENDINLINE
 
FUNCTION SetIcon: Iconname$ // use a 16x16 icon
LOCAL hwnd, icon
INLINE
hwnd = u32_GetActiveWindow();
icon = shell32_LoadIcon(hwnd,Iconname_Str.c_str(),0);
u32_SetClassLong(hwnd,-14,icon);
ENDINLINE
ENDFUNCTION
How can i create a Icon?
1.open Paint
2.set the pictersize to 16x16
3.draw something
4.save as "name.ico", press ENTER

DONE!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

bigsofty

Excellent stuff and a good example of interfacing with the Win32 API! :)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Hemlos

#3
Thanks for the code Schranz0r, good stuff...
I built something to go with it...no need to add icon.ico to a directory....just hardwire the sprite with this.
With a glowing backdrop too
Screenshot at bottom of my post.

Code (Hardwired Internal Window Icon Sprite Builder) Select

// --------------------------------- //
// Project: Hard Wired Window Icon Builder
// Start: Sunday, August 10, 2008
// IDE Version: 5.341
//
//Icon with Background and Foreground Layers
// windows Only WIN32
//
//You must Call InitializeWindowIcon() Before Main Loop:
InitializeWindowIcon()

//Main Loop:
WHILE TRUE
PRINT "test",10,10
SHOWSCREEN
WEND

//___________________________________________________________//
FUNCTION InitializeWindowIcon:
//- Build A New Window Icon Sprite, with colors and Background!
//- Created By Hemlos@GLBasic.com
//- 2d Window Icon Builder Created By Hemlos@GLBasic.com
//- Additional Credits:
//- Thank you (Schranz0r) for the INLINE C++ Window Icon Draw

//Syntax: InitializeWindowIcon()
// Place Function call before main loop!

//Background Colors: (Edit Center and Edge color)
LOCAL Icon_Center_Color=RGB(255,0,0)  // red
LOCAL Icon_Edge_Color=RGB(40,40,40)    // grey
LOCAL ICON$="icon.ico" //icon name
DIM SPRITE$ [16]

//Preset Foreground Colors: (Edit Icon Image With Color Presets)
// 0=Black W=White R=Red G=Green B=Blue C=Cyan Y=Yellow P=Purple
SPRITE$[0] ="................"
SPRITE$[1] =".......CC......."
SPRITE$[2] ="......CCCC......"
SPRITE$[3] =".....CCCCCC....."
SPRITE$[4] =".....CCCCCC....."
SPRITE$[5] =".....CCCCCC....."
SPRITE$[6] ="......CCCC......"
SPRITE$[7] =".......CC......."
SPRITE$[8] =".......CC......."
SPRITE$[9] =".......CC......."
SPRITE$[10]="................"
SPRITE$[11]=".......CC......."
SPRITE$[12]="......CCCC......"
SPRITE$[13]=".......CC......."
SPRITE$[14]="................"
SPRITE$[15]="................"

//Build Icon Background -Nothing to edit
ALPHAMODE -1
STARTPOLY -1
POLYVECTOR 7, 7, -1, -1, Icon_Center_Color
POLYVECTOR  0,   0,  -1,  -1, Icon_Edge_Color
POLYVECTOR   0, 15,  -1, -1, Icon_Edge_Color
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, Icon_Center_Color
POLYVECTOR   0, 15,  0, 0, Icon_Edge_Color
POLYVECTOR 15, 15, 0, 0, Icon_Edge_Color
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, Icon_Center_Color
POLYVECTOR   15, 15,  0, 0, Icon_Edge_Color
POLYVECTOR 15, 0, 0, 0, Icon_Edge_Color
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, Icon_Center_Color
POLYVECTOR   15, 0,  0, 0, Icon_Edge_Color
POLYVECTOR 0, 0, 0, 0, Icon_Edge_Color
ENDPOLY

//Build Icon Foreground -You can add more colors in this loop
LOCAL SPRITECOL, SPRITEROW, PixelColor, Pixel$
ALPHAMODE 1
FOR SPRITEROW=0 TO 15
FOR SPRITECOL=0 TO 15
Pixel$=MID$(SPRITE$[SPRITEROW],SPRITECOL,1)
IF Pixel$="."; PixelColor=RGB(0,0,0); ENDIF//black
IF Pixel$="W"; PixelColor=RGB(255,255,255); ENDIF//white
IF Pixel$="R"; PixelColor=RGB(255,0,0); ENDIF//red
IF Pixel$="G"; PixelColor=RGB(0,255,0); ENDIF//green
IF Pixel$="B"; PixelColor=RGB(0,0,255); ENDIF//blue
IF Pixel$="Y"; PixelColor=RGB(255,255,0); ENDIF//yellow
IF Pixel$="P"; PixelColor=RGB(255,0,255); ENDIF//Purple
IF Pixel$="C"; PixelColor=RGB(0,255,255); ENDIF//Cyan
//Add more Colors here, use lines above for example.
SETPIXEL SPRITECOL, SPRITEROW, PixelColor
NEXT
NEXT

//Build and Save Window Icon -Nothing to edit
GRABSPRITE 0, 0,0, 16,16
SAVESPRITE ICON$,0
BLACKSCREEN

//Draw window icon: -Code below, written by Schranz0r -Nothing to edit
LOCAL hwnd,icon
INLINE
DECLARE_C_ALIAS(u32_GetActiveWindow,"user32.dll", "GetActiveWindow",(),int);
DECLARE_C_ALIAS(u32_SetClassLong,"user32.dll", "SetClassLongA",(int,int,int), void);
DECLARE_C_ALIAS(shell32_LoadIcon,"Shell32.dll", "ExtractIconA",(int,const char*,int), int);
hwnd = u32_GetActiveWindow();
icon = shell32_LoadIcon(hwnd,ICON_Str.c_str(),0);
u32_SetClassLong(hwnd,-14,icon);
ENDINLINE
ENDFUNCTION


I tried to animate it..it works but i found a memory leak with this line:
   icon = shell32_LoadIcon(hwnd,ICON_Str.c_str(),0);
Commit charge and kernal memory flood slowly till it stops running and sends default icon to the window.
heres the code for the animation, maybe you can fix it...i tried and failed...
Code (Animated Hardwired Window Icon...BUGGED DONT USE, ONLY A TEST) Select

// --------------------------------- //
// Project: ANIMATED Hard Wired Window Icon -BUGGED DONT USE, ONLY TEST
// Start: Sunday, August 10, 2008
// IDE Version: 5.341
//
//Icon with Background and Foreground Layers
// Optional, Marquee Animated Window Icon
// windows Only WIN32
//
//To Build your own icon: Edit in InitializeWindowIcon()
//Edit the sprite block to change the foregound.
//Edit the Background Center and Edge Colors

LIMITFPS 60; AUTOPAUSE FALSE

//You must Call these variables, and
// the InitializeWindowIcon() First,
// Before Main Loop:
DIM SPRITE$ [16]
GLOBAL CenterColor=RGB(255,0,0)
GLOBAL EdgeColor=RGB(40,40,40)
GLOBAL ICON$="icon.ico"
InitializeWindowIcon(ICON$)

WHILE TRUE
AnimateIcon(ICON$,FALSE,TRUE) //Notes:
//Icon_Name(icon.ico), Animate_foreground(BOOL), Animate_Background(BOOL)
//This must be the first line in the main loop,
// OR youll get strange screen effects.
//This function blankscreens.

//Main Loop:
PRINT "test",10,10
SHOWSCREEN
WEND

//___________________________________________________________//
FUNCTION InitializeWindowIcon: Iconname$
//- Build A New Window Icon Sprite, with colors and Background!
//- Created By Hemlos@GLBasic.com
//- 2d Window Icon Builder Created By Hemlos@GLBasic.com
//- Additional Credits:
//- Thank you (Schranz0r) for the INLINE C++ Window Icon Draw

//Syntax: InitializeWindowIcon("icon.ico")
// Place Function call before main loop!

//Background Colors: (Edit Center and Edge color)

//Preset Foreground Colors: (Edit Icon Image With Color Presets)
// 0=Black W=White R=Red G=Green B=Blue C=Cyan Y=Yellow P=Purple
SPRITE$[0] ="................"
SPRITE$[1] =".......CC......."
SPRITE$[2] ="......CCCC......"
SPRITE$[3] =".....CCCCCC....."
SPRITE$[4] =".....CCCCCC....."
SPRITE$[5] =".....CCCCCC....."
SPRITE$[6] ="......CCCC......"
SPRITE$[7] =".......CC......."
SPRITE$[8] =".......CC......."
SPRITE$[9] =".......CC......."
SPRITE$[10]="................"
SPRITE$[11]=".......CC......."
SPRITE$[12]="......CCCC......"
SPRITE$[13]=".......CC......."
SPRITE$[14]="................"
SPRITE$[15]="................"

//Build Icon Background -Nothing to edit
ALPHAMODE -1
STARTPOLY -1
POLYVECTOR 7, 7, -1, -1, CenterColor
POLYVECTOR  0,   0,  -1,  -1, EdgeColor
POLYVECTOR   0, 15,  -1, -1, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   0, 15,  0, 0, EdgeColor
POLYVECTOR 15, 15, 0, 0, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   15, 15,  0, 0, EdgeColor
POLYVECTOR 15, 0, 0, 0, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   15, 0,  0, 0, EdgeColor
POLYVECTOR 0, 0, 0, 0, EdgeColor
ENDPOLY

//Build Icon Foreground -You can add more colors in this loop
LOCAL SPRITECOL, SPRITEROW, PixelColor, Pixel$
ALPHAMODE 1
FOR SPRITEROW=0 TO 15
FOR SPRITECOL=0 TO 15
Pixel$=MID$(SPRITE$[SPRITEROW],SPRITECOL,1)
IF Pixel$="."; PixelColor=RGB(0,0,0); ENDIF//black
IF Pixel$="W"; PixelColor=RGB(255,255,255); ENDIF//white
IF Pixel$="R"; PixelColor=RGB(255,0,0); ENDIF//red
IF Pixel$="G"; PixelColor=RGB(0,255,0); ENDIF//green
IF Pixel$="B"; PixelColor=RGB(0,0,255); ENDIF//blue
IF Pixel$="Y"; PixelColor=RGB(255,255,0); ENDIF//yellow
IF Pixel$="P"; PixelColor=RGB(255,0,255); ENDIF//Purple
IF Pixel$="C"; PixelColor=RGB(0,255,255); ENDIF//Cyan
//Add more Colors here, use lines above for example.
SETPIXEL SPRITECOL, SPRITEROW, PixelColor
NEXT
NEXT

//Build and Save Window Icon -Nothing to edit
GRABSPRITE 0, 0,0, 16,16
SAVESPRITE Iconname$,0
BLACKSCREEN
//Draw window icon: -Code below, written by Schranz0r -Nothing to edit
LOCAL hwnd,icon
INLINE
DECLARE_C_ALIAS(u32_GetActiveWindow,"user32.dll", "GetActiveWindow",(),int);
DECLARE_C_ALIAS(u32_SetClassLong,"user32.dll", "SetClassLongA",(int,int,int), void);
DECLARE_C_ALIAS(shell32_LoadIcon,"Shell32.dll", "ExtractIconA",(int,const char*,int), int);
hwnd = u32_GetActiveWindow();
icon = shell32_LoadIcon(hwnd,Iconname_Str.c_str(),0);
u32_SetClassLong(hwnd,-14,icon);
ENDINLINE
ENDFUNCTION


FUNCTION AnimateIcon: Iconname$ , AnimatedFG, AnimatedBG
STATIC alpha,alphablend
//Icon_Name(icon.ico), Animate_foreground(BOOL), Animate_Background(BOOL)
//Build Animated Icon-dont Edit anything in this function
//it is using the Variables set in InitializeWindowIcon()
//edit the icon in the InitializeWindowIcon Function
IF AnimatedBG
IF alphablend=1
alpha = alpha+0.01
IF alpha >= -0.1 THEN alphablend=-1
ENDIF
IF alphablend<=0
alpha = alpha-0.01
IF alpha <= -0.8 THEN alphablend=1
ENDIF
ELSE
alpha=0
ENDIF
ALPHAMODE alpha
STARTPOLY -1
POLYVECTOR 7, 7, -1, -1, CenterColor
POLYVECTOR  0,   0,  -1,  -1, EdgeColor
POLYVECTOR   0, 15,  -1, -1, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   0, 15,  0, 0, EdgeColor
POLYVECTOR 15, 15, 0, 0, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   15, 15,  0, 0, EdgeColor
POLYVECTOR 15, 0, 0, 0, EdgeColor
ENDPOLY
STARTPOLY -1
POLYVECTOR  7,   7,  0,  0, CenterColor
POLYVECTOR   15, 0,  0, 0, EdgeColor
POLYVECTOR 0, 0, 0, 0, EdgeColor
ENDPOLY

//Build Icon Foreground -You can add more colors in this loop
LOCAL SPRITECOL, SPRITEROW, PixelColor, Pixel$
STATIC IconMover
ALPHAMODE 1
IconMover=IconMover+0.25
IF IconMover>16 THEN IconMover=-16
FOR SPRITEROW=0 TO 15
FOR SPRITECOL=0 TO 15
Pixel$=MID$(SPRITE$[SPRITEROW],SPRITECOL,1)
IF Pixel$="."; PixelColor=RGB(0,0,0); ENDIF//black
IF Pixel$="W"; PixelColor=RGB(255,255,255); ENDIF//white
IF Pixel$="R"; PixelColor=RGB(255,0,0); ENDIF//red
IF Pixel$="G"; PixelColor=RGB(0,255,0); ENDIF//green
IF Pixel$="B"; PixelColor=RGB(0,0,255); ENDIF//blue
IF Pixel$="Y"; PixelColor=RGB(255,255,0); ENDIF//yellow
IF Pixel$="P"; PixelColor=RGB(255,0,255); ENDIF//Purple
IF Pixel$="C"; PixelColor=RGB(0,255,255); ENDIF//Cyan
//Add more Colors here, use lines above for example.
IF AnimatedFG;
SETPIXEL SPRITECOL+IconMover, SPRITEROW, PixelColor
ELSE
SETPIXEL SPRITECOL, SPRITEROW, PixelColor
ENDIF

NEXT
NEXT

//Build and Save Window Icon -Nothing to edit
GRABSPRITE 0, 0,0, 16,16
SAVESPRITE Iconname$,0
BLACKSCREEN
LOCAL hwnd,icon
INLINE
DECLARE_C_ALIAS(u32_GetActiveWindow,"user32.dll", "GetActiveWindow",(),int);
DECLARE_C_ALIAS(u32_SetClassLong,"user32.dll", "SetClassLongA",(int,int,int), void);
DECLARE_C_ALIAS(shell32_LoadIcon,"Shell32.dll", "ExtractIconA",(int,const char*,int), int);
hwnd = u32_GetActiveWindow();
icon = shell32_LoadIcon(hwnd,Iconname_Str.c_str(),0);
u32_SetClassLong(hwnd,-14,icon);
ENDINLINE
ENDFUNCTION





[attachment deleted by admin]
Bing ChatGpt is pretty smart :O

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Hemlos

Hi Schranzor,
I think this thread is obsolete now.
I dont mind if you erase my posts to remove the thread.
Bing ChatGpt is pretty smart :O

Schranz0r

CLOSED !

You can now set your own icon with GLBasic, see -> HELPFILE ( IDE press F1 )
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard