;{ [Function] GuiButtonIcon ;{ ; Fanatic Guru ; Version 2023 04 08 ; ; #Requires AutoHotkey v2.0.2+ ; ; FUNCTION to Assign an Icon to a Gui Button ; ;------------------------------------------------ ; ; Method: ; GuiButtonIcon(Handle, File, Index, Options) ; ; Parameters: ; 1) {Handle} HWND handle of Gui button or the Gui button object ; 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: ; MyGui := Gui() ; MyButton := MyGui.Add('Button', 'w70 h38', 'Save') ; GuiButtonIcon(MyButton, 'shell32.dll', 259, 's32 a1 r2') ; MyGui.Show ;} GuiButtonIcon(Handle, File, Index := 1, Options := '') { RegExMatch(Options, 'i)w\K\d+', &W) ? W := W.0 : W := 16 RegExMatch(Options, 'i)h\K\d+', &H) ? H := H.0 : H := 16 RegExMatch(Options, 'i)s\K\d+', &S) ? W := H := S.0 : '' RegExMatch(Options, 'i)l\K\d+', &L) ? L := L.0 : L := 0 RegExMatch(Options, 'i)t\K\d+', &T) ? T := T.0 : T := 0 RegExMatch(Options, 'i)r\K\d+', &R) ? R := R.0 : R := 0 RegExMatch(Options, 'i)b\K\d+', &B) ? B := B.0 : B := 0 RegExMatch(Options, 'i)a\K\d+', &A) ? A := A.0 : A := 4 W *= A_ScreenDPI / 96, H *= A_ScreenDPI / 96 button_il := Buffer(20 + A_PtrSize) normal_il := DllCall('ImageList_Create', 'Int', W, 'Int', H, 'UInt', 0x21, 'Int', 1, 'Int', 1) NumPut('Ptr', normal_il, button_il, 0) ; Width & Height NumPut('UInt', L, button_il, 0 + A_PtrSize) ; Left Margin NumPut('UInt', T, button_il, 4 + A_PtrSize) ; Top Margin NumPut('UInt', R, button_il, 8 + A_PtrSize) ; Right Margin NumPut('UInt', B, button_il, 12 + A_PtrSize) ; Bottom Margin NumPut('UInt', A, button_il, 16 + A_PtrSize) ; Alignment SendMessage(BCM_SETIMAGELIST := 5634, 0, button_il, Handle) Return IL_Add(normal_il, File, Index) }