Adding icon to exe

Windows specific forum
ly47
User
User
Posts: 23
Joined: Mon May 05, 2014 6:51 pm

Adding icon to exe

Post by ly47 »

Hi
I'm trying to add an icon (*.ico) to exe program.
Compiler Option > use icon.
The exe file have an icon ,but when executing the exe file it does not appear at the upper left window.
Please help.
Thanks
ly

Code: Select all

EnableExplicit

Declare SetWinColor(hWnd, clr)
Declare addStringCombo()
Declare xHiWord(a.l) 
Declare xLoWord(a.l) 

Global wc.WNDCLASSEX, hInstance, msg.MSG,  hCb, title.s, hue$=Space(6)
; hue$ = Space(8)
hInstance = GetModuleHandle_(#Null)

Enumeration
	#Button_1
	#Button_2
	#Button_3
	#Combo_1
EndEnumeration

#Style = #WS_OVERLAPPEDWINDOW | #WS_VISIBLE

Procedure WindowCallback(hWnd, uMsg, wParam, lParam) 
	Protected hBtn1, hBtn2, hBtn3, Result, indx
	
	Select uMsg 
			
		Case #WM_CREATE
						
			hBtn1 = CreateWindowEx_(0, "Button", "Exit", #WS_CHILD | #WS_VISIBLE, 520, 328, 60, 25, hWnd, #Button_1, hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "RED", #WS_CHILD | #WS_VISIBLE, 440, 328, 60, 25, hWnd, #Button_2,hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "BLUE", #WS_CHILD | #WS_VISIBLE, 360, 328, 60, 25, hWnd, #Button_3, hInstance, 0)
			hCb = CreateWindowEx_(0, "combobox", 0, #WS_CHILD | #WS_VISIBLE |#WS_TABSTOP|#CBS_DROPDOWNLIST|#WS_VSCROLL,
			                    250, 328, 90, 235, hWnd, #Combo_1, hInstance, 0)
			addStringCombo()
			
		Case #WM_COMMAND
			
			Select xLoWord(wParam) 
				
				Case #Button_1
					
; 					Debug "#Button_1"
					
					PostQuitMessage_(0)
					
				Case #Button_2
					
; 					Debug "#Button_2"
					
					SetWinColor(hWnd,RGB(233,95,85))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Button_3
					
; 					Debug "#Button_3"
					
					SetWinColor(hWnd, RGB(85,95,233))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Combo_1 
					
					If xHiWord(wParam) = #CBN_SELCHANGE
						
						indx = SendMessage_(hcb,#CB_GETCURSEL,0,0)
						
						SendMessage_(hCb, #CB_GETLBTEXT,indx, @hue$)
						
						title = "Simple -  " + hue$
						
						SendMessage_(hWnd,#WM_SETTEXT,0,@title)
						
						Select indx
								
							Case 0	;default
								
								SetWinColor(hWnd,RGB(255,255,255))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 1	;red
								
 								SetWinColor(hWnd,RGB(233,95,85))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 2  ;blue

								SetWinColor(hWnd, RGB(85,95,233))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
						EndSelect
											
					EndIf
					
			EndSelect

		Case #WM_CLOSE 
			DestroyWindow_(hWnd) 
			Result  = 0 
		Case #WM_DESTROY 
			PostQuitMessage_(0) 
			Result  = 0 
		Default 
			Result  = DefWindowProc_(hWnd, uMsg, wParam, lParam) 
	EndSelect 
	ProcedureReturn Result 
EndProcedure  

Define.s appName
appName = "Win32" 

	wc\style         = #CS_HREDRAW | #CS_VREDRAW 
	wc\cbsize  = SizeOf(WNDCLASSEX) 
	wc\lpfnWndProc  = @WindowCallback() 
	wc\hInstance     = hInstance 
	wc\hIcon         = LoadIcon_(0, #IDI_APPLICATION ) 
	wc\hCursor  = LoadCursor_(0,#IDC_ARROW)
	wc\hbrBackground  = #COLOR_WINDOW + 1
	wc\lpszMenuName  = 0
	wc\lpszClassName  = @appName
	
	If ( RegisterClassEx_( @wc ) = #False ) 
        MessageBox_(#Null, "Failed to register wc!", appName, #MB_ICONERROR )
        End 1
    EndIf 

Define hWnd  = CreateWindowEx_(0, appName, "Simple", #Style, 100, 100, 600, 450, 0, 0, hInstance, 0) 

ShowWindow_(hWnd,  #SW_SHOWDEFAULT) 
UpdateWindow_(hWnd)
SetForegroundWindow_(hWnd)

While GetMessage_(msg, 0, 0, 0 ) 
	TranslateMessage_(msg) 
	DispatchMessage_(msg) 
Wend
    
; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Procedure SetWinColor(hWnd, clr)
	Static obj, hbr

	hbr = CreateSolidBrush_(clr)
	obj  = SetClassLongPtr_(hWnd ,#GCL_HBRBACKGROUND ,hbr)
	DeleteObject_(obj)
	RedrawWindow_(hWnd, 0, 0, 0)
	
	ProcedureReturn 0
EndProcedure

Procedure addStringCombo()
	SendMessage_(hCb,#CB_ADDSTRING,0, "DEF")
	SendMessage_(hCb,#CB_ADDSTRING,0, "RED")
	SendMessage_(hCb,#CB_ADDSTRING,0,"BLUE")
	SendMessage_(hCb,#CB_SETCURSEL,0,0)
EndProcedure


Procedure xHiWord(a.l) 
	ProcedureReturn Int(a / $10000) 
EndProcedure 

Procedure xLoWord(a.l) 
	ProcedureReturn Int(a - (Int(a/$10000)*$10000)) 
EndProcedure
True29
User
User
Posts: 64
Joined: Sun Feb 03, 2013 1:50 am

Re: Adding icon to exe

Post by True29 »

perhaps wrong size of icon ?

my icon has 48x48
greets.
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Adding icon to exe

Post by TI-994A »

ly47 wrote:I'm trying to add an icon (*.ico) to exe program.
Compiler Option > use icon.
That's the right way. Most likely just an incompatible-format issue.

Staying with your API motif, give this a try:

Code: Select all

SendMessage_(WindowID(wndNo), #WM_SETICON, 0, LoadImage(0, "yourIcon.ico"))
:D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Adding icon to exe

Post by ts-soft »

Add the second line to your code:

Code: Select all

Define hWnd  = CreateWindowEx_(0, appName, "Simple", #Style, 100, 100, 600, 450, 0, 0, hInstance, 0)
SendMessage_(hWnd, #WM_SETICON, 0, LoadIcon_(hInstance, 1))
ShowWindow_(hWnd,  #SW_SHOWDEFAULT)
UpdateWindow_(hWnd)
SetForegroundWindow_(hWnd)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
ly47
User
User
Posts: 23
Joined: Mon May 05, 2014 6:51 pm

Re: Adding icon to exe

Post by ly47 »

Hi all
Thanks
Hi ts-soft
I have used your addition code and it works !!!
Thanks
ly
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Re: Adding icon to exe

Post by RASHAD »

Using your code just use your own Icon file

Code: Select all

   wc\style        = #CS_HREDRAW | #CS_VREDRAW 
   wc\cbsize       = SizeOf(WNDCLASSEX) 
   wc\lpfnWndProc  = @WindowCallback() 
   wc\hInstance    = hInstance 
   wc\hIcon        = LoadImage_(0,"e:\Vacuum.ico",#IMAGE_ICON,16,16,#LR_LOADFROMFILE)
   wc\hCursor      = LoadCursor_(0,#IDC_ARROW)
   wc\hbrBackground  = #COLOR_WINDOW + 1
   wc\lpszMenuName  = 0
   wc\lpszClassName  = @appName
Egypt my love
Post Reply