[Function] GuiButtonIcon

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

[Function] GuiButtonIcon

06 Feb 2014, 14:32

GuiButtonIcon

This function allows an icon to be assigned to a Gui Button using the button's hwnd to identify the button.

Code: Select all

;{ GuiButtonIcon
; Fanatic Guru
; 2014 05 31
; Version 2.0
;
; FUNCTION to Assign an Icon to a Gui Button
;
;------------------------------------------------
;
; Method:
;   GuiButtonIcon(Handle, File, Options)
;
;   Parameters:
;   1) {Handle} 	HWND handle of Gui button
;   2) {File} 		File containing icon image
;   3) {Index} 		Index of icon in file
;						Optional: Default = 1
;   4) {Options}	Single letter flag followed by a number with multiple options delimited by a space
;						W = Width of Icon (default = 16)
;						H = Height of Icon (default = 16)
;						S = Size of Icon, Makes Width and Height both equal to Size
;						L = Left Margin
;						T = Top Margin
;						R = Right Margin
;						B = Botton Margin
;						A = Alignment (0 = left, 1 = right, 2 = top, 3 = bottom, 4 = center; default = 4)
;
; Return:
;   1 = icon found, 0 = icon not found
;
; Example:
; Gui, Add, Button, w70 h38 hwndIcon, Save
; GuiButtonIcon(Icon, "shell32.dll", 259, "s30 a1 r2")
; Gui, Show
;
GuiButtonIcon(Handle, File, Index := 1, Options := "")
{
	RegExMatch(Options, "i)w\K\d+", W), (W="") ? W := 16 :
	RegExMatch(Options, "i)h\K\d+", H), (H="") ? H := 16 :
	RegExMatch(Options, "i)s\K\d+", S), S ? W := H := S :
	RegExMatch(Options, "i)l\K\d+", L), (L="") ? L := 0 :
	RegExMatch(Options, "i)t\K\d+", T), (T="") ? T := 0 :
	RegExMatch(Options, "i)r\K\d+", R), (R="") ? R := 0 :
	RegExMatch(Options, "i)b\K\d+", B), (B="") ? B := 0 :
	RegExMatch(Options, "i)a\K\d+", A), (A="") ? A := 4 :
	Psz := A_PtrSize = "" ? 4 : A_PtrSize, DW := "UInt", Ptr := A_PtrSize = "" ? DW : "Ptr"
	VarSetCapacity( button_il, 20 + Psz, 0 )
	NumPut( normal_il := DllCall( "ImageList_Create", DW, W, DW, H, DW, 0x21, DW, 1, DW, 1 ), button_il, 0, Ptr )	; Width & Height
	NumPut( L, button_il, 0 + Psz, DW )		; Left Margin
	NumPut( T, button_il, 4 + Psz, DW )		; Top Margin
	NumPut( R, button_il, 8 + Psz, DW )		; Right Margin
	NumPut( B, button_il, 12 + Psz, DW )	; Bottom Margin	
	NumPut( A, button_il, 16 + Psz, DW )	; Alignment
	SendMessage, BCM_SETIMAGELIST := 5634, 0, &button_il,, AHK_ID %Handle%
	return IL_Add( normal_il, File, Index )
}

Sample code showing the usage of function:

Code: Select all

Gui, Add, Button, w22 h22 hwndIcon
if !GuiButtonIcon(Icon, "some.exe") ; Example of Icon not found
    GuiButtonIcon(Icon, "shell32.dll") ; Not Found then do this

Gui, Add, Button, w22 h22 hwndIcon
GuiButtonIcon(Icon, A_AhkPath)
Gui, Add, Button, w22 h22 hwndIcon
GuiButtonIcon(Icon, "shell32.dll", 23)

Gui, Add, Button, w38 h38 hwndIcon1
Gui, Add, Button, w38 h38 hwndIcon2
Gui, Add, Button, w70 h38 hwndIcon3, Open
Gui, Add, Button, w70 h38 hwndIcon4, % " Save"
Gui, Add, Button, w70 h60 hwndIcon5, % "            Cut`n`n DANGER"
GuiButtonIcon(Icon1, "shell32.dll", 1, "s32")
GuiButtonIcon(Icon2, "imageres.dll", 46, "s32 a1")
GuiButtonIcon(Icon3, "shell32.dll", 46, "s32 a0 l2")
GuiButtonIcon(Icon4, "shell32.dll", 259, "s32 a1 r2")
GuiButtonIcon(Icon5, "shell32.dll", 260, "w40 h22 r20 b20")

Gui, Show

Return

GuiEscape:
GuiClose:
Exitapp

GuiButtonIcon(Handle, File, Index := 1, Options := "")
{
	RegExMatch(Options, "i)w\K\d+", W), (W="") ? W := 16 :
	RegExMatch(Options, "i)h\K\d+", H), (H="") ? H := 16 :
	RegExMatch(Options, "i)s\K\d+", S), S ? W := H := S :
	RegExMatch(Options, "i)l\K\d+", L), (L="") ? L := 0 :
	RegExMatch(Options, "i)t\K\d+", T), (T="") ? T := 0 :
	RegExMatch(Options, "i)r\K\d+", R), (R="") ? R := 0 :
	RegExMatch(Options, "i)b\K\d+", B), (B="") ? B := 0 :
	RegExMatch(Options, "i)a\K\d+", A), (A="") ? A := 4 :
	Psz := A_PtrSize = "" ? 4 : A_PtrSize, DW := "UInt", Ptr := A_PtrSize = "" ? DW : "Ptr"
	VarSetCapacity( button_il, 20 + Psz, 0 )
	NumPut( normal_il := DllCall( "ImageList_Create", DW, W, DW, H, DW, 0x21, DW, 1, DW, 1 ), button_il, 0, Ptr )	; Width & Height
	NumPut( L, button_il, 0 + Psz, DW )		; Left Margin
	NumPut( T, button_il, 4 + Psz, DW )		; Top Margin
	NumPut( R, button_il, 8 + Psz, DW )		; Right Margin
	NumPut( B, button_il, 12 + Psz, DW )	; Bottom Margin	
	NumPut( A, button_il, 16 + Psz, DW )	; Alignment
	SendMessage, BCM_SETIMAGELIST := 5634, 0, &button_il,, AHK_ID %Handle%
	return IL_Add( normal_il, File, Index )
}

The v2 version of this function can be found here: viewtopic.php?f=83&t=115871

FG
Last edited by FanaticGuru on 07 Apr 2023, 12:51, edited 5 times in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: [Function] GuiButtonIcon

06 Feb 2014, 15:10

i copied and ran the "sample code" but nothing happened. :geek:
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

06 Feb 2014, 16:06

Guest10 wrote:i copied and ran the "sample code" but nothing happened. :geek:
Thanks for the heads up.

In my stupidity in cut/pasting the example code I forgot to put Gui, Show so I was creating the Gui with buttons and all the stuff that required some thought but then did not paste the simplest line to display it.

Editted the original post to add the necessary Gui, Show.

BTW, "shell32.dll" and "imageres.dll" contain hundreds of useful Window icons that should be available and in path on most computers making them easy and fairly reliable to use. But Icons can also be found in many other files even executables like GuiButtonIcon(Icon, "C:\Program Files (x86)\Steam\steam.exe") for example.


FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: [Function] GuiButtonIcon

06 Feb 2014, 17:36

thanks, it works now. :D
Therobotmd
Posts: 7
Joined: 06 Jan 2014, 03:50

Re: [Function] GuiButtonIcon

07 Feb 2014, 17:37

Very nice. Easy and small.
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: [Function] GuiButtonIcon

14 Feb 2014, 21:06

Looks useful. Thanks.
Dragonslayr
Posts: 17
Joined: 24 Feb 2014, 10:12

Re: [Function] GuiButtonIcon

22 Mar 2014, 22:32

Thank You!
I really appreciate the Documentation in the Function Code!!

Seems to work nicely!
Dragonslayr
Posts: 17
Joined: 24 Feb 2014, 10:12

Re: [Function] GuiButtonIcon

24 Mar 2014, 11:42

Is there a way this function can return a default icon if no icon is found for a file?

I do not understand good enough how the function achieves it's goal to add this feature. :(
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

24 Mar 2014, 13:48

UPDATED SCRIPT ON FIRST PAGE

Change Log: Version 1.1
Function now returns true or false depending on whether icon image was found
Added in example a use of this returned information to retry with a different icon if the first failed

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: [Function] GuiButtonIcon

25 Mar 2014, 14:42

Doesn't the following essentially do the same thing using the native Gui commands?

Gui, Add, Picture, x%x_location% y%y_location% w-1 h10 hwndPicHandle gGuiCall, %A_ScriptDir%\263.ico

The width and height parameters can adjust the rendered size. The only thing missing is flagging a missing icon file.

Relayer
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

25 Mar 2014, 16:57

Relayer wrote:Doesn't the following essentially do the same thing using the native Gui commands?

Gui, Add, Picture, x%x_location% y%y_location% w-1 h10 hwndPicHandle gGuiCall, %A_ScriptDir%\263.ico

The width and height parameters can adjust the rendered size. The only thing missing is flagging a missing icon file.

Relayer
It really depends on whether you want a button or a picture. Both can do something when you click on them but they are not exactly the same. For one a button provides a graphical cue that it is a button and meant to be click on also a button provides graphical feedback when you click on it.

Below is code that shows both. They look different and behave different when you click on them.

Code: Select all

Gui, Add, Picture, w-1 h22 hwndPicHandle, shell32.dll
Gui, Add, Text,x50 yp, Picture
Gui, Add, Button, xm w22 h22 hwndIcon
GuiButtonIcon(Icon, "shell32.dll") 
Gui, Add, Text,x50 yp, Button

Gui, Show, w100 h100

Return

GuiEscape:
GuiClose:
Exitapp

GuiButtonIcon(Handle, File, Index := 1, Size := 22, Margin := 6, Align := 4)
{
    Size -= Margin
    Psz := A_PtrSize = "" ? 4 : A_PtrSize, DW := "UInt", Ptr := A_PtrSize = "" ? DW : "Ptr"
    VarSetCapacity( button_il, 20 + Psz, 0 )
    NumPut( normal_il := DllCall( "ImageList_Create", DW, Size, DW, Size, DW, 0x21, DW, 1, DW, 1 ), button_il, 0, Ptr )
    NumPut( Align, button_il, 16 + Psz, DW )
    SendMessage, BCM_SETIMAGELIST := 5634, 0, &button_il,, AHK_ID %Handle%
    return IL_Add( normal_il, File, Index )
}
In general buttons normally make better buttons.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: [Function] GuiButtonIcon

26 Mar 2014, 15:59

Thank you... I see the benefits now.

It looks like the image list facility is designed around icons. Can your script be adapted to place any picture file on a button?

Relayer
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

26 Mar 2014, 17:21

Relayer wrote: It looks like the image list facility is designed around icons. Can your script be adapted to place any picture file on a button?
I know PNGs can be done. Just make sure the PNG is where it will be found or include the full path. Also choose an index of 0.

GuiButtonIcon(Icon, "C:\Users\Guru\Desktop\fire.png", 0)

I don't know about other picture formats, I have not tried them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
TedStriker
Posts: 8
Joined: 30 May 2014, 13:46

Re: [Function] GuiButtonIcon

30 May 2014, 13:59

Hi,

thank you for this very nice function. Adding icons to buttons without making clickable pictures is pretty useful. :)

I've a question about the margin parameter. As far as I understood, it should move the icon, but it only makes them smaller.

This is what I get with GuiButtonIcon(Icon1, Icons.icl, 11, 16, 0, 0)
Image

And this is what I get with GuiButtonIcon(Icon1, Icons.icl, 11, 16, 6, 0)
Image

Maybe I just miss-understood this parameter. Is there any way to get the icon a bit more to the right? (Using AHK 1.1.15 unicode 32b)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

31 May 2014, 01:55

TedStriker wrote:Maybe I just miss-understood this parameter. Is there any way to get the icon a bit more to the right? (Using AHK 1.1.15 unicode 32b)
Margin does just shrink the icon.

If a button is created that is w38 h38 then you can create a GuiButtonIcon with a size of 38 and margin of 6. This actually creates an icon that is 32 which leaves a margin of 3 spaces on all sides if the icon is centered on the button.

The margin parameter was added so that you could use the same size number for both the button and the icon size and the icon would be shrunk by default to be a little smaller than the button.

These two GuiButtonIcon commands do functionally exactly the same thing.
Gui, Add, Button, w38 h38 hwndIcon
GuiButtonIcon(Icon, "shell32.dll", 23, 38) ; size 38 with default of 6 margin
GuiButtonIcon(Icon, "shell32.dll", 23, 32, 0) ; size 32 with 0 margin

But the first is suppose to be easier because you can use the same size for the button and icon and let the margin provide the shrinkage amount to have the icon be a little smaller to fit on the button.

This all works fine when the icon is centered on the button but when you use the align to an edge this can be confusing because information in the Image List determines how far the icon will be from the edge.

This information in the Image List can most likely be altered with NumPut the same way the alignment is altered in the image structure.

When I get time I will fool around with it. I just hate having to pass up to 4 more parameters to the function. It is more about how to get all the information to the function in an elegant way.

I might just rewrite it to have options like Gui where all the options are sent in one parameter with stuff like w38 h48 a4 mt5 mb5 ml10 mr8 for width 38, height 48, alignment 4, margin top 5, margin bottom 5, margin left 10, margin right 8.

The point of the whole function is to keep it simple and easy but I still might be able to do that and provide more control to those that need it. The function will end up being more code to sort out the options than actually creating the button icon.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
TedStriker
Posts: 8
Joined: 30 May 2014, 13:46

Re: [Function] GuiButtonIcon

31 May 2014, 17:18

Thank you for the explanation. I thought the margin would be from the left, as I configured the alignment to be left. Guess it's not that easy. ;-) No need to enhance your function. It's good as it is. Being an amateur script writer I truly enjoy putting icons on buttons so easily.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

31 May 2014, 21:41

Updated Script in First Post

Change Log: Version 2.0, 2014-05-31
Changed the way options are passed to the script to allow more flexibility.
Now an icon can have individual left, top, right and bottom margins that allow for more control over positioning of the icon on the button. The margin is on the icon image not the button.
This margin is like transparent padding that is being added to the image. By using a center alignment and then adding this margin to the image you can effectively push the image where ever you want on the button.
It is possible to use button, icon and margin sizes that create clipping of the image if sizes are used that will not all fit within the size of the button.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Function] GuiButtonIcon

31 May 2014, 22:05

TedStriker wrote:Thank you for the explanation. I thought the margin would be from the left, as I configured the alignment to be left. Guess it's not that easy. ;-) No need to enhance your function. It's good as it is. Being an amateur script writer I truly enjoy putting icons on buttons so easily.
Also welcome to the boards TedStriker.

I noticed this was your first post here.

I am serious... and don't call me Shirley.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
TedStriker
Posts: 8
Joined: 30 May 2014, 13:46

Re: [Function] GuiButtonIcon

05 Jun 2014, 14:16

After reading your post I didn't think you'd change the function, but now it can do even more and is easier to use. :shock: Thank you! I hope your function gets included in AHK or at least mentioned in the help file.
I am serious... and don't call me Shirley.
I love these old ZAZ movies/series... "Cigarettes?" - "Yes, I know." :-D
enthused

Re: [Function] GuiButtonIcon

25 Dec 2014, 14:14

This is great and makes for better user experience and enhances GUI look.
I'm almost certain this can be done, but I'm not sure how:
Can Icon be selected at run time from an exe file? Say the button acts as OpenDialog and opens for file selection with only exe files can be selected. What the file the user selects, then that Icon from that file is what is on the button.

I think it can be done with other function help to extract Icon from exe file and so on.
Thank you for this nice addition.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 74 guests