Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

ILButton() - image buttons with text, states, alignment


  • Please log in to reply
52 replies to this topic
VanDiZ
  • Members
  • 21 posts
  • Last active: Mar 20 2013 07:40 PM
  • Joined: 25 Aug 2011
First of all thank u vary mutch tkoi for your great shearing. :)

I was wondering for something like your function.

But, in my computer the ilbuttondemo.ahk in not working, it shows

an error msg that it is unable to include ILButton11.ahk

Both the function and the demo ahk are in the same folder and the full path to the working directory contains no space.

Whats the problem then ??

.....................

My OS is Windows 7 Ultimate Build 7600

and my computer is a laptop, Acer aspire 4736

Please reply soon

HighRiseWriter
  • Members
  • 36 posts
  • Last active: Jun 29 2015 06:40 AM
  • Joined: 15 Jan 2011

First of all thank u vary mutch tkoi for your great shearing. :)

I was wondering for something like your function.

But, in my computer the ilbuttondemo.ahk in not working, it shows

an error msg that it is unable to include ILButton11.ahk

Both the function and the demo ahk are in the same folder and the full path to the working directory contains no space.

Whats the problem then ??

.....................

My OS is Windows 7 Ultimate Build 7600

and my computer is a laptop, Acer aspire 4736

Please reply soon




I did the same thing. Check to see if you named the script ILButton.ahk and not ILButton11.ahk

HighRiseWriter
  • Members
  • 36 posts
  • Last active: Jun 29 2015 06:40 AM
  • Joined: 15 Jan 2011
My AHK app uses audio icons for play, pause, stop, and volume.

I want one button to share the play and pause icons. So far I can make the pause icon replace the play icon when the button is pressed however it then immediately fades back to the play icon.

Any suggestions on how to keep an icon present after the button is pressed?

zeus19
  • Members
  • 49 posts
  • Last active: Feb 24 2017 12:59 PM
  • Joined: 14 Apr 2011
First of all, thanks tkoi! And can you tell me something. Is there anyway to pack the icons into .exe file? (no need to include icons file with .exe to display the icon on the GUI button)

Cragaha
  • Members
  • 265 posts
  • Last active: Jan 04 2016 02:24 AM
  • Joined: 19 Nov 2010
@zeus19,
As far as I know you can include the icons in the compiler but that's not a good idea, instead you should make a dll with your icons, there is a function on the forum by SKAN to do so.

zeus19
  • Members
  • 49 posts
  • Last active: Feb 24 2017 12:59 PM
  • Joined: 14 Apr 2011
Nazzal, thanks for you help, I'll try!

BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011
Salvete! I tried ILButtons 1.1 with AutoHotkey_L 1.1.05.06, and it works perfectly with an .icl file. (I used Sib Icon Editor and imported a bunch of png files).

Thank you very much! This is nice!

I am wondering, however, if it is possible for the button outline to be hidden until a mouseover.

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
First I have to thank the author for this great function! I used it in my project and it has given it a much better look.

An user asked me why the icons weren't showing for him with the source script and I found out that the function doesn't work with 64bit version of AHK_L.
I don't know enough about DllCall right now to understand this, so I wonder if anyone knows a way to make it work in AHK_L x64 as well...

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
This is working with AHK U32/Vista-32 and U64/Win7-64:

/*
Title: ILButton
Version: 1.1
Author: tkoi <https://ahknet.autohotkey.com/~tkoi>
License: GNU GPLv3 <http://www.opensource.org/licenses/gpl-3.0.html>

Function: ILButton()
    Creates an imagelist and associates it with a button.
Parameters:
    hBtn   - handle to a buttton
    images - a pipe delimited list of images in form "file:zeroBasedIndex"
               - file must be of type exe, dll, ico, cur, ani, or bmp
               - there are six states: normal, hot (hover), pressed, disabled, defaulted (focused), and stylushot
                   - ex. "normal.ico:0|hot.ico:0|pressed.ico:0|disabled.ico:0|defaulted.ico:0|stylushot.ico:0"
               - if only one image is specified, it will be used for all the button's states
               - if fewer than six images are specified, nothing is drawn for the states without images
               - omit "file" to use the last file specified
                   - ex. "states.dll:0|:1|:2|:3|:4|:5"
               - omitting an index is the same as specifying 0
               - note: within vista's aero theme, a defaulted (focused) button fades between images 5 and 6
    cx     - width of the image in pixels
    cy     - height of the image in pixels
    align  - an integer between 0 and 4, inclusive. 0: left, 1: right, 2: top, 3: bottom, 4: center
    margin - a comma-delimited list of four integers in form "left,top,right,bottom"

Notes:
    A 24-byte static variable is created for each IL button
    Tested on Vista Ultimate 32-bit SP1 and XP Pro 32-bit SP2.

Changes:
  v1.1
    Updated the function to use the assume-static feature introduced in AHK version 1.0.48
*/

ILButton(hBtn, images, cx=16, cy=16, align=4, margin="1,1,1,1") {
	static
	static i = 0
	local himl, v0, v1, v2, v3, ext, hbmp, hicon
	i ++

	himl := DllCall("ImageList_Create", "Int",cx, "Int",cy, "UInt",0x20, "Int",1, "Int",5, "UPtr")
	Loop, Parse, images, |
		{
		StringSplit, v, A_LoopField, :
		if not v1
			v1 := v3
		v3 := v1
		SplitPath, v1, , , ext
		if (ext = "bmp") {
			hbmp := DllCall("LoadImage", "UInt",0, "Str",v1, "UInt",0, "UInt",cx, "UInt",cy, "UInt",0x10, "UPtr")
			DllCall("ImageList_Add", "Ptr",himl, "Ptr",hbmp, "Ptr",0)
			DllCall("DeleteObject", "Ptr", hbmp)
			}
		else {
			DllCall("PrivateExtractIcons", "Str",v1, "Int",v2, "Int",cx, "Int",cy, "PtrP",hicon, "UInt",0, "UInt",1, "UInt",0)
			DllCall("ImageList_AddIcon", "Ptr",himl, "Ptr",hicon)
			DllCall("DestroyIcon", "Ptr", hicon)
			}
		}
	; Create a BUTTON_IMAGELIST structure
	VarSetCapacity(struct%i%, A_PtrSize + (5 * 4) + (A_PtrSize - 4), 0)
	NumPut(himl, struct%i%, 0, "Ptr")
	Loop, Parse, margin, `,
		NumPut(A_LoopField, struct%i%, A_PtrSize + ((A_Index - 1) * 4), "Int")
	NumPut(align, struct%i%, A_PtrSize + (4 * 4), "UInt")
	; BCM_FIRST := 0x1600, BCM_SETIMAGELIST := BCM_FIRST + 0x2
	PostMessage, 0x1602, 0, &struct%i%, , ahk_id %hBtn%
	Sleep 1 ; workaround for a redrawing problem on WinXP
	}

Prefer ahkscript.org for the time being.


Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
It worked perfectly! Thank you very much, just me!!!
I'll updated my script and add your contribution to the credits.

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


User1554
  • Guests
  • Last active:
  • Joined: --
Hey guys,

anyone know how I can make a certain button "defaulted" on startup to make it look active? Thanks!

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
Here's a version based on just me's that also takes absolute paths:
ILButton(hBtn, images, cx=16, cy=16, align=4, margin="1,1,1,1")
{
	static
	static i = 0
	local himl, v0, v1, v2, v3, ext, hbmp, hicon
	i ++

	himl := DllCall("ImageList_Create", "Int",cx, "Int",cy, "UInt",0x20, "Int",1, "Int",5, "UPtr")
	Loop, Parse, images, |
	{
		Pos := InStr(A_LoopField, ":", false, 3)
		if(pos)
		{
			v1 := SubStr(A_LoopField, 1, pos - 1)
			v2 := SubStr(A_LoopField, pos + 1)
		}
		else
			v1 := A_LoopField
		SplitPath, v1, , , ext
		if(ext = "bmp")
		{
			hbmp := DllCall("LoadImage", "UInt",0, "Str",v1, "UInt",0, "UInt",cx, "UInt",cy, "UInt",0x10, "UPtr")
			DllCall("ImageList_Add", "Ptr",himl, "Ptr",hbmp, "Ptr",0)
			DllCall("DeleteObject", "Ptr", hbmp)
		}
		else
		{
			DllCall("PrivateExtractIcons", "Str",v1, "Int",v2, "Int",cx, "Int",cy, "PtrP",hicon, "UInt",0, "UInt",1, "UInt",0)
			DllCall("ImageList_AddIcon", "Ptr",himl, "Ptr",hicon)
			DllCall("DestroyIcon", "Ptr", hicon)
		}
	}
	; Create a BUTTON_IMAGELIST structure
	VarSetCapacity(struct%i%, A_PtrSize + (5 * 4) + (A_PtrSize - 4), 0)
	NumPut(himl, struct%i%, 0, "Ptr")
	Loop, Parse, margin, `,
	NumPut(A_LoopField, struct%i%, A_PtrSize + ((A_Index - 1) * 4), "Int")
	NumPut(align, struct%i%, A_PtrSize + (4 * 4), "UInt")
	; BCM_FIRST := 0x1600, BCM_SETIMAGELIST := BCM_FIRST + 0x2
	PostMessage, 0x1602, 0, &struct%i%, , ahk_id %hBtn%
	Sleep 1 ; workaround for a redrawing problem on WinXP
}


  • Guests
  • Last active:
  • Joined: --
Nice fragman
I use this function in many of my scripts, the only down side for it is if you don't specify -Theme for the button it wont animate pushing the button image.

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
I don't see this issue, I get press animations without -Theme as well.

  • Guests
  • Last active:
  • Joined: --

I don't see this issue, I get press animations without -Theme as well.


When not using -Theme the button itself will animate the push effect, but the image wont, it will stay in the same place.