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

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

=====>>> 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


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

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.

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?

Its in step 2 to 4

nice one... all sorted now.
cheers

For me I just have to double click the bat file and it works dine.

How do I do this with a non windowed application that doesn't use maxgui

this is for a std. blitzmax window. not sure how it works on maxgui.

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

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())


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.