Gui, Add, Button, w64 h64 hwndhButton1
Gui, Add, Button, x+5 yp+13 w200 h38 hwndhButton2, % " Этот текст будет виден"
; картинка из png файла
SetImageOnButton(hButton1, "icon.png", 60, 60)
; иконка из dll
SetImageOnButton(hButton2, "Shell32", 24, 24, IMAGE_ICON := 1, 44)
Gui, Show
Return
GuiClose:
ExitApp
SetImageOnButton(hwnd, imageFilePath, w := 16, h := 16, imageType := 0, iconNumber := 0) {
static BS_ICON := 0x40
, BS_BITMAP := 0x80
, IMAGE_ICON := 1
, IMAGE_BITMAP := 0
, BM_SETIMAGE := 0xF7
if !hImage := LoadPicture(imageFilePath, "w" . w . " h" . h . (iconNumber ? " Icon" . iconNumber : ""), imageType)
throw "Failed to load image"
ControlGetText, text,, ahk_id %hwnd%
if (text = "") {
WinSet, Style, % "+" . (imageType = IMAGE_ICON ? BS_ICON : BS_BITMAP), ahk_id %hwnd%
if ErrorLevel
throw "Failed to assign style"
}
SendMessage, BM_SETIMAGE, imageType, hImage,, ahk_id %hwnd%
if ErrorLevel
imageType = IMAGE_ICON ? DllCall("DestroyIcon" , "Ptr", ErrorLevel)
: DllCall("DeleteObject", "Ptr", ErrorLevel)
}