updated test script in attempt to retain image transparency with png file loaded from AxC
Code:
; --------------------------------------------
; modified version of script posted by SKAN
; http://www.autohotkey.com/forum/viewtopic.php?t=29593&postdays=0&postorder=asc&highlight=axcunpacktomem&start=21
; --------------------------------------------
IfNotExist, png.axc
UrlDownloadToFile,http://arian.suresh.googlepages.com/png.axc, png.axc
Gui, +LastFound
GUI1:=WinExist(), hDC:=DllCall("GetDC",UInt,Gui1)
; start GDI+ -----------------------------------------------------------------------------
DllCall( "LoadLibrary", Str,"gdiplus" )
VarSetCapacity(si, 16, 0), si := Chr(1)
DllCall( "gdiplus\GdiplusStartup", UIntP,pToken, UInt,&si, UInt,0 )
; ----------------------------------------------------------------------------------------
FileReadEx( IMG, "png.axc", 32016,95488 )
hBitmap := AGB_gdiBitmap( IMG, 32016, wxh )
AddGraphicButton("SampleButton1", hBitmap, "h144 w144 gMyButton")
Gui, Show
; stop GDI+ -----------------------------------------------------------------------------
DllCall( "gdiplus\GdiplusShutdown", UInt,pToken )
DllCall( NumGet(NumGet(1*pStream)+8 ), UInt,pStream )
; ----------------------------------------------------------------------------------------
Return
GuiClose:
ExitApp
Return
MyButton:
MsgBox, Graphic button clicked :)
return
AGB_gdiBitmap( byref buffer, nsize, byref wxh ) {
; Sean : http://www.autohotkey.com/forum/viewtopic.php?p=147029#147029
; modified by corrupt - image transparency when using with button controls in AHK
hData := DllCall("GlobalAlloc", UInt,2, UInt, nSize )
pData := DllCall("GlobalLock", UInt,hData )
DllCall( "RtlMoveMemory", UInt,pData, UInt,&Buffer, UInt,nSize )
DllCall( "GlobalUnlock", UInt,hData )
DllCall( "ole32\CreateStreamOnHGlobal", UInt,hData, Int,True, UIntP,pStream )
DllCall( "gdiplus\GdipCreateBitmapFromStream", UInt,pStream, UIntP,pBitmap )
bkcol := DllCall( "user32\GetSysColor", Unt, (COLOR_BTNFACE := 15))
AA_FormatInteger=%A_FormatInteger%
SetFormat Integer, H
bkcol := bkcol + 0
SetFormat Integer, %AA_FormatInteger%
Loop % (8-StrLen(bkcol))
bkcol:="0x0" . SubStr(bkcol, (StrLen(bkcol)-A_Index-2))
bkcol := "0xFF" . SubStr(bkcol, 7, 2) . SubStr(bkcol, 5, 2) . SubStr(bkcol, 3, 2)
DllCall( "gdiplus\GdipCreateHBITMAPFromBitmap", UInt, pBitmap, UIntP, hBitmap, UInt, bkcol)
DllCall("gdiplus\GdipGetImageWidth" , "Uint", pBitmap, "UintP", nW)
DllCall("gdiplus\GdipGetImageHeight", "Uint", pBitmap, "UintP", nH), wxh := nW "x" nH
DllCall( "gdiplus\GdipDisposeImage", UInt,pBitmap )
return "0." . hbitmap
}
FileReadEx( ByRef V,F,B,O ) {
return varsetcapacity(v,b,0)-o+(h:=dllcall("_lopen",str,f,int,0))-h+dllcall("_llseek",uint
,h,uint,o,int,0)-dllcall("_lread",uint,h,str,v,int,b)+dllcall("_lclose",uint,h) ? -1 : &v
}
; *******************************************************************
; AddGraphicButton.ahk
; *******************************************************************
; Version: 2.2 Updated: May 20, 2007
; by corrupt
; *******************************************************************
; VariableName = variable name for the button
; ImgPath = Path to the image to be displayed
; Options = AutoHotkey button options (g label, button size, etc...)
; bHeight = Image height (default = 32)
; bWidth = Image width (default = 32)
; *******************************************************************
; note:
; - calling the function again with the same variable name will
; modify the image on the button
; *******************************************************************
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%
bDC := DllCall("user32\GetDC", UInt,%VariableName%_hwnd)
DllCall( "gdi32\SetBkMode", UInt,bDC, UInt,1)
}
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
}
Seems to test ok on XP Pro SP3. Any feedback (works/doesn't work) would be appreciated if anyone has a chance to try it out
Edit: bug found/fixed - ABGR Hex wasn't padded