[Function] GuiButtonIcon

Post your working scripts, libraries and tools
FanaticGuru
Posts: 1093
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] [Expand] [Download] (GuiButtonIcon.ahk)GeSHi © Codebox Plus



Sample code showing the usage of function:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

FG
Last edited by FanaticGuru on 31 May 2014, 21:32, edited 4 times in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
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:
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
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.
jballi
Posts: 476
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. :(
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
Relayer
Posts: 112
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
FanaticGuru
Posts: 1093
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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
Relayer
Posts: 112
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
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
TedStriker
Posts: 5
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)
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
TedStriker
Posts: 5
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.
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
FanaticGuru
Posts: 1093
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

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
TedStriker
Posts: 5
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”

Who is online

Users browsing this forum: cocobanana and 15 guests