Hello!
Firstly, thanks to Corrupt and everyone involved in making this – it's great!
I'm trying to create one of these buttons on a GUI which is sometimes destroyed and recreated. In the case below, the picture button will only appear the first time the GUI is created:
Code:
loop
{
AddGraphicButton("SampleButton", A_WorkingDir . "\testbmp.bmp", "h30 w140 gMyButton", 30, 140)
gui,add,button,x100 y100,hello
gui,show
gui +lastfound
winwaitclose
msgbox gui destroyed
}
return
mybutton:
gui,destroy
return
;=====
;Picture / image button: AddGraphicButton.ahk - http://www.autohotkey.com/forum/topic4047.html
;=====
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
}
This can be resolved by either …
OPTION 1:
Removing the following line in AddGraphicButton.ahk:
Quote:
If (%VariableName%_hwnd = "")
Code:
Code:
loop
{
AddGraphicButton("SampleButton", A_WorkingDir . "\testbmp.bmp", "h30 w140 gMyButton", 30, 140)
gui,add,button,x100 y100,hello
gui,show
gui +lastfound
winwaitclose
msgbox gui destroyed
}
return
mybutton:
gui,destroy
return
;=====
;Picture / image button: AddGraphicButton.ahk - http://www.autohotkey.com/forum/topic4047.html
;=====
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
}
OPTION 2:
Setting a new variable name every time:
Code:
i=0
loop
{
i+=1
AddGraphicButton("SampleButton" i, A_WorkingDir . "\testbmp.bmp", "h30 w140 gMyButton", 30, 140)
gui,add,button,x100 y100,hello
gui,show
gui +lastfound
winwaitclose
msgbox gui destroyed
}
return
mybutton:
gui,destroy
return
;=====
;Picture / image button: AddGraphicButton.ahk - http://www.autohotkey.com/forum/topic4047.html
;=====
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
}
Option 1 is more desirable than Option 2, but my question is …
Will removing the line “If (%VariableName%_hwnd = "")” do anything nasty...?