Getting a .exe icon On Window, NO EXTERNAL FILES!

BlitzMax Forums/BlitzMax Tutorials/Getting a .exe icon On Window, NO EXTERNAL FILES!

Yahfree(Posted 2007) [#1]
=====>>> THIS IS FOR WINDOWS <<<=====


I asked how to do this, and a few people helped out and this is their Idea's/help mixed together in a working method.


this small tutorial will show you how to get a .exe icon for your exe file and on the window WITHOUT using external files with the finished product.

Step 1----------------------------------
make a folder: "IconMaker" i like to keep this near my project folders, so i can easely grab the produced .o file and drop it in the project folder.

Step 2----------------------------------
now get these 3 files in this "IconMaker" folder:
1: "icon.ico" ;your icon (32x32 .ico) this will be the icon on the exe file.

2: "resfile.rc" ; create this with notepad with the following line:
101 ICON icon.ico


3: "windres.exe" ; from your MinGW/bin

Step 3-----------------------------------
Now, open notepad and add the following line:

windres -i resfile.rc -o icon_import.o


save this as build.bat in the same "IconMaker" folder

Step 4-----------------------------------
Now simply double click the .bat file to produce your icon .o file.


this was hard to set up, but now making a new icon res file is as easy as dropping a new icon into that folder, and deleting the other one, then re double clicking the .bat file.


Step 5------------------------------------
Now to get it on the window call the following function right after Graphics.
' -----------------------------------------------------------------------------
' SetIcon
' -----------------------------------------------------------------------------
Function SetIcon(iconname$, TheWindow%)	
	?Win32
	Local icon=ExtractIconA(TheWindow,iconname,0)
	Local WM_SETICON = $80
	Local ICON_SMALL = 0
	Local ICON_BIG = 1
	sendmessage(TheWindow, WM_SETICON, ICON_BIG, icon)
	?
End Function

'call it like this
SetIcon(AppFile, GetActiveWindow())

you'll also need the following win32 externs:
'Needed externs
?win32
Extern "win32"
	Function ExtractIconA%(hWnd%,File$z,Index%)
	Function GetActiveWindow%()
	Function SendMessage:Int(hWnd:Int,MSG:Int,wParam:Int,lParam:Int) = "SendMessageA@16"
End Extern
?


(Credits to grey alien for that function, this will extract the icon out of your exe file and display it on the window.)


now the result is a completely independant compiled exe that needs no external files to display its icon.

heres an example:
Import "icon_import.o"

'Needed externs
?win32
Extern "win32"
	Function ExtractIconA%(hWnd%,File$z,Index%)
	Function GetActiveWindow%()
	Function SendMessage:Int(hWnd:Int,MSG:Int,wParam:Int,lParam:Int) = "SendMessageA@16"
End Extern
?

Graphics 800,600
SetIcon(AppFile,GetActiveWindow())

While Not AppTerminate() = True
Cls


Flip
Wend
End



Function SetIcon(iconname$, TheWindow%)	
	?Win32
	Local icon=ExtractIconA(TheWindow,iconname,0)
	Local WM_SETICON = $80
	Local ICON_SMALL = 0
	Local ICON_BIG = 1
	sendmessage(TheWindow, WM_SETICON, ICON_BIG, icon)
	?
End Function



PantsOn(Posted 2007) [#2]
I've not tried this method yet.. but i did try Grey Aliens method.
The problem that I have is that it displays the icon in runtime only. But not when your exe is sitting on the desktop or any shortcuts to your exe.

Whats the method for ataching the icon to the exe so that Windows sees it? (I've tried the old skool resource hacker method with no luck)

cheers


grable(Posted 2007) [#3]
The problem that I have is that it displays the icon in runtime only. But not when your exe is sitting on the desktop or any shortcuts to your exe.

Linking to the compiled resource object with the icon should do it, at least it works for me.


PantsOn(Posted 2007) [#4]
i'm going to play dumb here... ;-)

Linking to the compiled resource object with the icon should do it, at least it works for me.


what?
is that part of the above steps?


grable(Posted 2007) [#5]
Its in step 2 to 4


PantsOn(Posted 2007) [#6]
nice one... all sorted now.
cheers


Grey Alien(Posted 2007) [#7]
For me I just have to double click the bat file and it works dine.


gameshastra(Posted 2008) [#8]
How do I do this with a non windowed application that doesn't use maxgui


Yahfree(Posted 2008) [#9]
this is for a std. blitzmax window. not sure how it works on maxgui.


gameshastra(Posted 2008) [#10]
Hi,
I have used
?Win32
Import "etchicon_import.o"
?
with successs.
The above given code also does the same.
But I still am not able to change the Icon which appears in the left top of the application window.
Regards,
Ramesh


Yahfree(Posted 2008) [#11]
Hey, maybe a bit late for responce, but...

Importing the object file embedds the icon into the compiled exe file... to place it on the window, you do step 5.. a function that calls out that embedded icon and places on the window.

Mind you, you need some windows external functions...

external win32 functions:

'Needed externs
?win32
Extern "win32"
	Function ExtractIconA%(hWnd%,File$z,Index%)
	Function GetActiveWindow%()
	Function SendMessage:Int(hWnd:Int,MSG:Int,wParam:Int,lParam:Int) = "SendMessageA@16"
End Extern
?


function that calls out the exe icon and places it on the window.

' -----------------------------------------------------------------------------
' SetIcon
' -----------------------------------------------------------------------------
Function SetIcon(iconname$, TheWindow%)	
	?Win32
	Local icon=ExtractIconA(TheWindow,iconname,0)
	Local WM_SETICON = $80
	Local ICON_SMALL = 0
	Local ICON_BIG = 1
	sendmessage(TheWindow, WM_SETICON, ICON_BIG, icon)
	?
End Function

'call it like this
SetIcon(AppFile, GetActiveWindow())



Johnsprogram(Posted 2008) [#12]
Is there a way to use the smaller icon? (The .iso had a 16x16 and a 32x32) Even if I replaced ICON_BIG (1) to ICON_SMALL (0), the icon still shows the 32x32 one squished to 16x16.


Vin78(Posted 2012) [#13]
I don't know why you guys are going to such lengths. I just use a downloaded program that looks inside the .exe, allows you to select any icon replace rebuild exactly the same executable but with a jazzy icon.


GfK(Posted 2012) [#14]
Because this method sets the window icon, exe icon, icon when you alt-tab, task bar icon - everything.

Plus i only have to do this once, not every time I build a new exe.

Last edited 2012


GaryV(Posted 2012) [#15]
I don't know why you guys are going to such lengths. I just use a downloaded program that looks inside the .exe, allows you to select any icon replace rebuild exactly the same executable but with a jazzy icon.


There is a right way to do things and a half-assed way to do things.


Vin78(Posted 2012) [#16]
Ah ok. I just find it the easiest route. I used to use Dark Basic and you couldn't change the internal icons for love nor money. If you did it wouldn't run. Anyone own a copy of the Grey Alien Framework? I'll give anyone £70 for it. Their licence that is. Not a copy.


matibee(Posted 2012) [#17]
Anyone own a copy of the Grey Alien Framework? I'll give anyone £70 for it. Their licence that is. Not a copy.


I hate to derail this thread (there's no email on Vin78's profile) but I did this after I missed out on Greys...

http://www.blitzbasic.com/Community/posts.php?topic=92656#1057028

and there are others. Please post elsewhere for any further info.

/derailment


Vin78(Posted 2012) [#18]
Ah that's great. Unfortunately I can't get it working. I have emailed you. I will have to post in the appropriate places in future :O!
Thanks Mati


Ts Inventions(Posted 2012) [#19]
Does anyone know how to do this (with Resource Hacker) for Biltz Basic?
Also, is this possible to do with JUST a program like Resource Hacker,
and not have to write extra code in your program to set the icon?

I'm just wondering here because so far I have gotten ZERO CLEAR ANSWERS.
So all real help for me is greatly appreciated!


caitsith2(Posted 2013) [#20]
Code modified for working in SuperStrict mode, and for multi-platform compilation.

' -----------------------------------------------------------------------------
' SetIcon
' -----------------------------------------------------------------------------
Function SetIcon(iconname$, TheWindow%)	
	?Win32
	Local icon:Int=ExtractIconA(TheWindow,iconname,0)
	Local WM_SETICON:Int = $80
	Local ICON_SMALL:Int = 0
	Local ICON_BIG:Int = 1
	sendmessage(TheWindow, WM_SETICON, ICON_BIG, icon)
	?
End Function

'call it like this
?Win32
SetIcon(AppFile, GetActiveWindow())
?


Everything else is the same as before. Because the call to SetIcon used a Win32 dependent function, it really should have been enclosed in the ?Win32 / ?.