Rewrite AddGraphicButton to accept PNG files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Rewrite AddGraphicButton to accept PNG files

05 Nov 2019, 18:55

I found a quite usefull function on the forum to add a image to a gui button.
https://autohotkey.com/board/topic/3816-graphic-buttons/
However, as far as i could test it, it only works on icon and bmp files.
Is it possible to rewrite it so it would also accept PNG files (or an other image with transparency functionality).

Another option would be to rewrite another similar function GuiButtonIcon, maybe this is easier?
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=1985

Code: Select all

AddGraphicButton("SampleButton1", A_WorkingDir . "\icons\Assembly.bmp", "h30 w140 gMyButton", 20, 20)
Gui, Show,, Bitmap Buttons 

Return 

AddGraphicButton(VariableName, ImgPath, Options="", bHeight=32, bWidth=32) 
{ 
	Global 
	Local ImgType, ImgType1, ImgPath0, ImgPath1, ImgPath2, hwndmode 
; BS_BITMAP := 128, IMAGE_BITMAP := 0, BS_ICON := 64, IMAGE_ICON := 1 
	Static LR_LOADFROMFILE := 16 
	Static BM_SETIMAGE := 247 
	Static NULL 
	SplitPath, ImgPath,,, ImgType1 
	If ImgPath is float 
	{ 
		ImgType1 := (SubStr(ImgPath, 1, 1)  = "0") ? "bmp" : "ico" 
		StringSplit, ImgPath, ImgPath,`. 
		%VariableName%_img := ImgPath2 
		hwndmode := true 
	} 
	ImgTYpe := (ImgType1 = "bmp") ? 128 : 64 
	If (%VariableName%_img != "") AND !(hwndmode) 
		DllCall("DeleteObject", "UInt", %VariableName%_img) 
	If (%VariableName%_hwnd = "") 
		Gui, Add, Button,  v%VariableName% hwnd%VariableName%_hwnd +%ImgType% %Options% 
	ImgType := (ImgType1 = "bmp") ? 0 : 1 
	If !(hwndmode) 
		%VariableName%_img := DllCall("LoadImage", "UInt", NULL, "Str", ImgPath, "UInt", ImgType, "Int", bWidth, "Int", bHeight, "UInt", LR_LOADFROMFILE, "UInt") 
	DllCall("SendMessage", "UInt", %VariableName%_hwnd, "UInt", BM_SETIMAGE, "UInt", ImgType,  "UInt", %VariableName%_img) 
	Return, %VariableName%_img ; Return the handle to the image 
} 

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 )
}
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Rewrite AddGraphicButton to accept PNG files  Topic is solved

06 Nov 2019, 08:02

Try this:

Code: Select all

icoFile := "D:\Icons\icon.ico"
pngFile := "D:\Icons\image.png"

BS_ICON      := 0x40
BS_BITMAP    := 0x80
IMAGE_BITMAP := 0
IMAGE_ICON   := 1
BM_SETIMAGE  := 0xF7

Gui, Add, Button, w100 h30 %BS_ICON% hwndhIconButton
Gui, Add, Button, x+5 yp wp hp %BS_BITMAP% hwndhImageButton

if !hIcon   := LoadPicture(icoFile, "h24 w-1", IMAGE_ICON)
   MsgBox, Can't load icon
if !hBitmap := LoadPicture(pngFile, "h24 w-1", IMAGE_BITMAP)
   MsgBox, Can't load image

if !(hIcon || hBitmap)
   ExitApp

SendMessage, BM_SETIMAGE, IMAGE_ICON  , hIcon  ,, ahk_id %hIconButton%
SendMessage, BM_SETIMAGE, IMAGE_BITMAP, hBitmap,, ahk_id %hImageButton%

Gui, Show
Return

GuiClose:
   ExitApp
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Rewrite AddGraphicButton to accept PNG files

06 Nov 2019, 15:28

Amazing, I did not know that it was this (If you have the code) simple :bravo:
@teadrinker: Many thanks!

I tried to search this in the Autohotkey Help, but could not find it.
This would be a good example to add to the Gui section of the Autohotkey help, images on the buttons make the Gui more userfriendly and seems to me an option that a lot of users are looking for.

I tried playing a little bit with the code, and I have the impression that it does not matter if you use 0x40 or 0x80.
Also the parameter IMAGE_BITMAP and IMAGE_ICON can be left out, does anybody knows what is their function is?

Code: Select all

PicFile := A_ScriptDir "\icons\Assembly.png"
Gui, Add, Button, 0x80 w40 h40 hwndhButton
if !hPic   := LoadPicture(PicFile, "h24 w-1")
	MsgBox, Can't load Picture
SendMessage, 0xF7 ,   , hPic ,, ahk_id %hButton%

Gui, Show
Return
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Rewrite AddGraphicButton to accept PNG files

06 Nov 2019, 16:03

AHK_user wrote: Also the parameter IMAGE_BITMAP and IMAGE_ICON can be left out, does anybody knows what is their function is?
Documents know:
LoadPicture() wrote:If this parameter is omitted or not a variable, the return value is always a bitmap handle (icons/cursors are converted if necessary). This is because reliably using or deleting an icon/cursor/bitmap handle requires knowing which type it is.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Rewrite AddGraphicButton to accept PNG files

13 Sep 2021, 10:01

I added it to a usefull function:

Code: Select all

#SingleInstance, Force

imageFile := "SHELL32.dll"
GuiAddImageButton(" hwndhIconButton", imageFile,  "icon4")

Gui, Show
Return

GuiClose:
ExitApp

GuiAddImageButton(Options, ImageFile, ImageOptions := ""){
	
	ImageType := (ImageFile ~= "\.(exe|ico)") ? IMAGE_ICON := 1 : IMAGE_BITMAP := 0
	BOptions := (ImageFile ~= "\.(exe|ico)") ? BS_ICON := 0x40 : BS_BITMAP := 0x80
	
	BHandle := RegexMatch(options, ".*\bhwnd(\w+).*", Match) ? Match1 : "hTemp"
	BOptions .= RegexMatch(options, ".*\bhwnd(\w+).*") ? "" : " hwnd" BHandle ; Add handle if not defined
	
	Gui, Add, Button, %Options% %BOptions%
	
	BHeight := RegexMatch(options, ".*\bh(\d+).*", Match) ? Match1 : 21 ; Assume height of 21 if not defined
	ImageOptions .= RegexMatch(ImageOptions, ".*\bh(\d+).*", Match) ? "" : (ImageFile ~= "\.(exe|ico)") ? " h" BHeight-8 : " h" BHeight-4 ; if not defined, fill button with image, the icon a little bit smaller
	
	if !hImage   := LoadPicture(ImageFile, ImageOptions, ImageType)
		MsgBox, Can't load file
	
	SendMessage, BM_SETIMAGE := 0xF7, ImageType  , hImage  ,, % "ahk_id " %BHandle%
	
	; Free the Image handle
	oDelete := ["DeleteObject", "DestroyIcon", "DestroyCursor"]
	DllCall(oDelete[ImageType+1], "ptr", hImage)
	
	return 
}
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Rewrite AddGraphicButton to accept PNG files

13 Sep 2021, 12:07

Awesome! Thanks for sharing this.

I was able to take a gdip drawing and use it as a image on the button. Wish more of this type of stuff was listed in the docs. I would have never found any of this or known that it existed if you hadn't posted when you did. :thumbup:

Code: Select all

#Include <GDIP>
#SingleInstance, Force
SetBatchlines, -1

GDIP_StartUp()

pBitmap := HB_BITMAP_MAKER()
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)

Gui, Add, Button, xm ym w200 h50 0x80 hwndButtonHwnd
SendMessage, 0xF7 , 0 , hBitmap,, % "ahk_id " ButtonHwnd

Gui, Show,

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp

HB_BITMAP_MAKER(){
	;Bitmap Created Using: HB Bitmap Maker
	pBitmap := Gdip_CreateBitmap( 40 , 40 ) , G := Gdip_GraphicsFromImage( pBitmap ) , Gdip_SetSmoothingMode( G , 2 )
	Brush := Gdip_CreateLineBrush( 8 , 6 , 32 , 33 , "0xFF333333" , "0xFF222222" , 1 ) , Pen := Gdip_CreatePenFromBrush( Brush , 3 ) , Gdip_DeleteBrush( Brush ) , Gdip_DrawLine( G , Pen , 10 , 10 , 30 , 30 ) , Gdip_DeletePen( Pen )
	Brush := Gdip_CreateLineBrush( 8 , 6 , 32 , 33 , "0xFF333333" , "0xFF222222" , 1 ) , Pen := Gdip_CreatePenFromBrush( Brush , 3 ) , Gdip_DeleteBrush( Brush ) , Gdip_DrawLine( G , Pen , 10 , 30 , 30 , 10 ) , Gdip_DeletePen( Pen )
	Brush := Gdip_CreateLineBrush( 8 , 2 , 30 , 37 , "0xFF333333" , "0xFF222222" , 1 ) , Pen := Gdip_CreatePenFromBrush( Brush , 8 ) , Gdip_DeleteBrush( Brush ) , Gdip_DrawEllipse( G , Pen , 3 , 3 , 32 , 32 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFF3399FF" , 4 ) , Gdip_DrawEllipse( G , Pen , 3 , 3 , 32 , 32 ) , Gdip_DeletePen( Pen )
	Brush := Gdip_CreateLineBrush( 8 , 5 , 29 , 34 , "0xFFF0F0F0" , "0xFF222222" , 1 ) , Pen := Gdip_CreatePenFromBrush( Brush , 2 ) , Gdip_DeleteBrush( Brush ) , Gdip_DrawEllipse( G , Pen , 3 , 3 , 32 , 32 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFF3399FF" , 1 ) , Gdip_DrawLine( G , Pen , 10 , 30 , 30 , 10 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFF3399FF" , 1 ) , Gdip_DrawLine( G , Pen , 10 , 10 , 28 , 28 ) , Gdip_DeletePen( Pen )
	Gdip_DeleteGraphics( G )
	return pBitmap
}
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Rewrite AddGraphicButton to accept PNG files

13 Sep 2021, 14:30

I just found the extreme similar version for V2, that adds methods to the gui object.
Even better :bravo: :bravo: :bravo:

viewtopic.php?t=86124
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Rewrite AddGraphicButton to accept PNG files

14 Sep 2021, 05:06

Hi

I think it is easier to implement the shared "GuiButtonic" function on this page: viewtopic.php?t=1985. Also accept .png.

The code to be implemented is as short as:

Code: Select all

Gui, CopyButtom:Add, Button, x1 y17 w64 h65 hwndIcon gCopyTexClip
GuiButtonIcon(Icon, "clipboard1.ico", 1, "s90")
Where the second line tells you the name of the icon and the size. Also accept .dll of icons.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Rohwedder, slowwd and 235 guests