Re: Upcoming Ahk2Exe Changes (2021)
Posted: 26 Jul 2021, 19:00
Let's help each other out
https://www.autohotkey.com/boards/
Code: Select all
;@Ahk2Exe-UpdateManifest 0, , , 1
;@Ahk2Exe-UpdateManifest 1, , , 1
Code: Select all
FileInstall, Icons\Sample.ico, % "EmptyVar" := ""
Gui, Add, Picture, x+10, % "HBITMAP: " . HBitMapFromResource("Icons\Sample.ico")
Code: Select all
#SingleInstance, force
Main:
SetWorkingDir, % A_ScriptDir
IconSrcPath := "c:\Windows\System32\OneDrive.ico"
;@Ahk2Exe-IgnoreBegin
if FileExist(IconSrcPath) {
if !FileExist("Icons")
FileCreateDir, Icons
if !FileExist("Sample.ico")
FileCopy, % IconSrcPath, Sample.ico
if !FileExist("Icons\Sample2.ico")
FileCopy, % IconSrcPath, Icons\Sample2.ico
} else {
MsgBox, Change IconSrcPath to a valid one and try again!
ExitApp
}
;@Ahk2Exe-IgnoreEnd
FileInstall, Sample.ico, % "EmptyVar" := ""
Gui, Add, Text, xm w100, Sample.ico:
Gui, Add, Picture, x+10, % "HBITMAP: " . HBitMapFromResource("Sample.ico")
Gui, Add, GroupBox, xm w300 h2
FileInstall, Icons\Sample2.ico, % "EmptyVar" := ""
Gui, Add, Text, xm w100, Icons\Sample2.ico:
Gui, Add, Picture, x+10, % "HBITMAP: " . HBitMapFromResource("Icons\Sample2.ico")
Gui, Add, GroupBox, xm w300 h2
; this would show up in compiled (Ahk2Exe >= 1.33.10) script, but not in uncompiled script:
; Gui, Add, Text, xm w100, Sample2.ico:
; Gui, Add, Picture, x+10, % "HBITMAP: " . HBitMapFromResource("Sample2.ico")
Gui Show
return
GuiClose:
ExitApp
; HBitMapFromResource (mentioned) from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?t=83589 , second post:
HBitmapFromResource(resName) {
if !A_IsCompiled
hBitmap := LoadPicture(resName, "GDI+")
else {
hMod := DllCall("GetModuleHandle", "Ptr", 0, "Ptr")
hRes := DllCall("FindResource", "Ptr", hMod, "Str", resName, "UInt", RT_RCDATA := 10, "Ptr")
resSize := DllCall("SizeofResource", "Ptr", hMod, "Ptr", hRes)
hResData := DllCall("LoadResource", "Ptr", hMod, "Ptr", hRes, "Ptr")
pBuff := DllCall("LockResource", "Ptr", hResData, "Ptr")
pStream := DllCall("Shlwapi\SHCreateMemStream", "Ptr", pBuff, "UInt", resSize)
Gdip := new GDIplus
pBitmap := Gdip.CreateBitmapFromStream(pStream)
hBitmap := Gdip.CreateHBITMAPFromBitmap(pBitmap)
Gdip.DisposeImage(pBitmap)
ObjRelease(pStream)
}
Return hBitmap
}
class GDIplus {
__New() {
if !DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
DllCall("LoadLibrary", "Str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", "UPtrP", pToken, "Ptr", &si, "Ptr", 0)
this.token := pToken
}
__Delete() {
DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
if hModule := DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
DllCall("FreeLibrary", "Ptr", hModule)
}
CreateBitmapFromStream(pStream) {
DllCall("gdiplus\GdipCreateBitmapFromStream", "Ptr", pStream, "PtrP", pBitmap)
Return pBitmap
}
CreateHBITMAPFromBitmap(pBitmap, background := 0xffffffff) {
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hbm, "UInt", background)
return hbm
}
DisposeImage(pBitmap) {
return DllCall("gdiplus\GdipDisposeImage", "Ptr", pBitmap)
}
}