Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Embed images in compiled AutoHotkey scripts?


  • Please log in to reply
73 replies to this topic

Poll: Would you be interested in the ability to have images embeded in compiled AutoHotkey scripts? (146 member(s) have cast votes)

Would you be interested in the ability to have images embeded in compiled AutoHotkey scripts?

  1. Voted Yes (183 votes [99.46%])

    Percentage of vote: 99.46%

  2. No (1 votes [0.54%])

    Percentage of vote: 0.54%

Vote Guests cannot vote
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

How would i do this with an Exe???


Did you browse the entire topic?
<!-- m -->http://www.autohotke... ... 202#147202<!-- m -->

Obi-Wahn
  • Members
  • 77 posts
  • Last active: Oct 29 2012 05:55 PM
  • Joined: 20 Apr 2006
Hi!

If I load a Image from a dll and load another one to overlay each other I'll get a rectengular field (instead of the transparency) with the gui color.

This happens with Icons and PNG's aswell...

Posted Image

So theres a workaround from Lexikos, but since this was three years ago, maybe there's a better way?!?

PS.: I'm using this functions to load pngs (and jpegs) and icons:

REF_ICON(rFile, rID, pX, pY, pW, pH, pLabel = false) {
	; From: http://www.autohotkey.com/forum/viewtopic.php?t=9980&p=147202#147202
	hModule := DllCall("LoadLibrary", Str, rFile)
	hBitmap := DllCall("LoadImageA", UInt, hModule, UInt, rID, UInt, (IMAGE_ICON := 0x1)
						, Int, pW, Int, pH, UInt, (LR_SHARED := 0x8000))
	Gui, Add, Picture, % "x" pX " y" pY " w" pW " h" pH (pLabel ? " g" pLabel : "") " " (SS_ICON := 0x3) " hWndPic1"
	SendMessage, (STM_SETIMAGE := 0x172), (IMAGE_ICON := 0x1), hBitmap, , ahk_id %Pic1%
	Return, Errorlevel
}

REF_IMAGE(rFile, rID, pX, pY, pW, pH, pLabel = false) {
	; From: http://www.autohotkey.com/forum/viewtopic.php?t=9980&p=147202#147202
	; And: http://www.autohotkey.com/forum/viewtopic.php?p=243145#243145
	hModule := DllCall("LoadLibrary", Str, rFile)
	hResource := DllCall("FindResource", UInt, hModule, UInt, rID, UInt, 10)
	nSize := DllCall("SizeofResource", UInt, hModule, UInt, hResource)
	hResData := DllCall("LoadResource", UInt, hModule, UInt, hResource)
	Buffer := DllCall("LockResource", UInt, hResData)
	; Converting Image to hBitmap (by Sean)
	; http://www.autohotkey.com/forum/viewtopic.php?t=22999
	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("LoadLibrary", Str, "gdiplus")
	VarSetCapacity(si, 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", UIntP, pToken, UInt, &si, UInt, 0)
	DllCall("gdiplus\GdipCreateBitmapFromStream", UInt, pStream, UIntP, pBitmap)
	DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", UInt, pBitmap, UIntP, hBitmap, UInt, 0)
	Gui, Add, Picture, % "x" pX " y" pY " w" pW " h" pH (pLabel ? " g" pLabel : "") " " (SS_BITMAP := 0xE) " hWndPic1"
	SendMessage, (STM_SETIMAGE := 0x172), (IMAGE_BITMAP := 0x0), hBitmap, , ahk_id %Pic1%
	elvl := Errorlevel
	DllCall("gdiplus\GdipDisposeImage", UInt, pBitmap)
	DllCall("gdiplus\GdiplusShutdown", UInt, pToken)
	DllCall(NumGet(NumGet(1*pStream)+8), UInt, pStream)
	Return, elvl
}

Tryoutpackage: <!-- m -->https://ahknet.autoh... ... r/ref2.zip<!-- m -->

THXIA

Malnas01
  • Members
  • 3 posts
  • Last active: Dec 21 2010 11:14 AM
  • Joined: 01 May 2010
whenever i edit the exe with resource hacker and i try to run it it says exe corrupt. i dont really get how to do all this can someone please explain step by step what i need to do to get my own image to display?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

whenever i edit the exe with resource hacker and i try to run it it says exe corrupt.


I suppose you are referring to compiled AutoHotkey script.
Instead of adding resources to the the compiled script, you add them
to ?:\Program Files\AutoHotkey\Compiler\AutoHotkeySC.bin.
Every compiled script thereafter will have those resources included.
Also, you need to turn-off UPX compression, if you need to access the resources from ResHacker.

Malnas01
  • Members
  • 3 posts
  • Last active: Dec 21 2010 11:14 AM
  • Joined: 01 May 2010
thanks that helped. How would i make it centred on a bigger gui? atm its only in the top right corner. or even better is there any way to scale it to fit the screen?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

is there any way to scale it to fit the screen?


If you are dealing with BITMAP, you may use CopyImage() to upscale images. For other image formats, you will have to use GDIplus.

Ritwik
  • Guests
  • Last active:
  • Joined: --
I see how this works via SendMessage. But could I add BMPs [or PNGs] to a menu [for menu icons], instead of a window? If so, how?

can u explain me the code for including bitmaps.. line by the line :D comments help me understand.. i would love some...
and please instruct how to include... more bitmaps... any helpful links to dllcall, hmodule, getmodulehandle stuff?


hModule  := DllCall( "GetModuleHandle", Str, A_ScriptFullPath ) ; what does this do  :oops:


A_ScriptFullpath is the executing file and is already loaded in memory. We need a handle to it so that we can refer the resources stored in it.

In case we need to refer a Bitmap stored in a Dll, we can get a handle to it with a DllCall to LoadLibrary. ( example will follow )

hBitmap1 := DllCall( "LoadImageA", UInt,hModule, UInt,007, UInt,(IMAGE_BITMAP:=0x0), Int,250, Int,250, UInt, (LR_SHARED := 0x8000) )  ; same again and image dimensions have to be hardcoded ??

If you specify 0 & 0 for X & Y, the original size would be loaded. Most of the time the user would be knowing the dimensions already unless he is writing a Resource Hacker in AHK ( which is possible! )

Gui, Add,Text, x0 y0 w0 h0 hwndPic1 +0xE  ; text or image?? why hwndPic1 ? which style number is this?

Text and Picture both are Static controls except a AHK picture control has the SS_Bitmap ( 0xE ) style. If we wanted to call a picture control we have to specify the picture file name as the third parameter or else the STM_SETIMAGE would not work. Therefore you either provide a dummy picture file as the third parameter or use SS_Bitmap in options of the picture control. I chose to use Text control with SS_Bitmap so that it is prominent enough to be noticed by the users. ( This means you may choose to add an empty picture control with SS_Bitmap in options. )

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap1,, ahk_id %Pic1% ; mumbo jumbo.. :oops:??

Every standard windows control can be controlled with SendMessages and this is one example.
From AHK when you call something like GuiControl,, MyPic, C:\MyPic.BMP to change the contents of a picture control, AHK will be internally using such messages to achieve the task.
These are the message that can be used on a Static Control

.. and here is an example code that calls a bitmap resource from Shell32.dll

:arrow: No need to compile it :

Gui, -Caption +AlwaysOnTop +Border +ToolWindow
Gui, Margin, 0, 0

Gui, Add, Picture, x0 y0 w0 h0 +0xE vMyPic1 hWndPic1 ; +0xE is SS_BITMAP

hModule  := DllCall( "LoadLibrary", Str,"Shell32.dll" )
hBitmap1 := DllCall( "LoadImageA", UInt,hModule, UInt,14351, UInt,(IMAGE_BITMAP:=0x0)
                    , Int,0, Int,0, UInt, (LR_SHARED := 0x8000) )

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap1,, ahk_id %Pic1%
GuiControlGet, MyPic1, Pos
Gui, Show, w%MyPic1W% h%MyPic1H%
Return

GuiClose:
GuiEscape:
 ExitApp
Return

Shell32.dll loads into the memory when Windows boots-up and yet I call LoadLibrary() only for the sake of a module handle.
I have not hardcoded the dimensions but have to ascertain the picture control's size to fit the GUI.

Any more? :)



iimraann
  • Members
  • 1 posts
  • Last active: Feb 27 2011 10:09 AM
  • Joined: 27 Feb 2011
@ SKAN
Thanks SKAN!! Just What I was looking for. :D

  • Guests
  • Last active:
  • Joined: --
Okay, ive read through the entire topic, and still am a little confused. Even know this will be handy in more complex ideas, all i want to do is add a jpg to an exe. The program i have uses a jpg as the background, and requires that the jpg is in the same dirrectory (or where ever). This means for me, i have 2 files, the exe and the jpg. Whats an easy way to add the picture to the exe so when i run the program, it will look inside the exe and find the pic?

Obi-Wahn
  • Members
  • 77 posts
  • Last active: Oct 29 2012 05:55 PM
  • Joined: 20 Apr 2006
Use ResHack to open AutoHotkeySC.bin or the .bin file you are using to compile the .ahk file.
Add all Resources to the .bin file and save it in the compiler directory using a new name. You can then select the edited .bin file in the AHK-Compiler.

You can either use the ansii version of my ref-lib, which you'll find in the forums, or the plain code which you'll have to search through one or two dozen topics, or you can use the 2.0 version of my ref-lib in case you using unicode, but that's still in developement phase.
You will however find a raw version of the lib in the german section of the AHK-Forums: <!-- m -->http://de.autohotkey... ... 8101#78101<!-- m -->

adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Hi all,

I know that this is a bit of an old thread, but this looks really interesting. But there are two things that I don't understand.
  • What does DllCall( NumGet(NumGet(1*pStream)+8 ), UInt,pStream ) do?
  • This is more of a general windows thing. I've always had problems trying to understand resources and have never used the as a result. The main problem is that I don't know where to get the referring resource id from. Are they just randomly assigned? If so, then I guess you have to use a string instead of an integer resource id?
Thanks for this thread. Look really good. I found a better res editor called "Resource Editor". It a very nice editor and is freeware written in Delphi (though I don't think the source is available). The project started in 2009 and was last updated Sept 2011 so it is fairly mature.


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
1. See [advanced] Using raw COM interfaces in AHK.
2. Resource identifiers are chosen by whoever inserted the resource into the executable file, and can be integers or strings.

For viewing/editing resources, I personally prefer Resource Hacker FX (which is Resource Hacker patched to fix some annoyances).

adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Thanks Lexikos, Will read up on that. I'm starting to understand the Resource IDs now. Hopefully more fully soon.

I found that the Resource Hacker/Resource Hacker FX was somewhat lacking (partial Unicode, no support for semi-transparent PNGs being the first 2 that I came across). Resource Editor is more complete, at least on those 2 fronts. But, to each their own. :)


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


wzxiaodu
  • Members
  • 4 posts
  • Last active: Dec 11 2013 03:56 PM
  • Joined: 30 Nov 2012

Foreword: Special thanks to Sean.



Dear Friends, :)

You will need my version of AutoHotkeySC.bin - 444KB ( 1.0.47.04 ) which has additional resources that are being referenced in the following example scripts:
Rename the default \AutoHotkey\Compiler\AutoHotkeySC.bin and download the above file into the same folder.

All the examples have to be compiled before execution.
Also you might want to rename UPX.EXE ( to turn off Compression ) if you want to explore the resultant EXE in ResHacker.

Referencing a BITMAP:



Gui, Margin, 0 , 0
hModule  := DllCall( "GetModuleHandle", Str, A_ScriptFullPath ) 
hBitmap1 := DllCall( "LoadImageA", UInt,hModule, UInt,[color=red]560[/color], UInt,(IMAGE_BITMAP:=0x0)
                  , Int,400, Int,300, UInt, (LR_SHARED := 0x8000) ) 

Gui, Add,Text, x0 y0 w0 h0 hwndPic1 +0xE

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap1, , ahk_id %Pic1%
Gui, Show, w400 h300, Gradient Background
Return

GuiClose:
 ExitApp
Referencing a WAVE:

hModule   := DllCall( "GetModuleHandle", Str, A_ScriptFullPath ) 
hResource := DllCall("FindResource", UInt,hModule, UInt,[color=red]666[/color], UInt,10 )
nSize     := DllCall("SizeofResource", UInt,hModule, UInt, hResource )

hResData  := DllCall("LoadResource", UInt,0, UInt,hResource )
Buffer    := DllCall("LockResource", "UInt", hResData )

Loop 10 { ; ringout.wav 
 DllCall("winmm.dll\sndPlaySoundA", UInt,Buffer, UINT, ( (SND_MEMORY:=0x4) | (SND_NODEFAULT:=0x2)) )
 Sleep 1000
}

ExitApp
Referencing a JPEG:

Gui, Margin, 0, 0 
Gui, Add, Text, x0 y0 w800 h600 +0xE hWndPic1 ; +0xE is SS_BITMAP 

hModule   := DllCall( "GetModuleHandle", Str, A_ScriptFullPath ) 
hResource := DllCall("FindResource", UInt,hModule, UInt,[color=red]555[/color], UInt,10)
nSize     := DllCall("SizeofResource", UInt,hModule, UInt, hResource)

hResData  := DllCall("LoadResource", UInt,hModule, UInt,hResource )
Buffer    := DllCall("LockResource", UInt, hResData )

; Converting Image data to hBITMAP ; Thanks Sean
; http://www.autohotkey.com/forum/viewtopic.php?t=22999

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( "LoadLibrary", Str,"gdiplus" )
VarSetCapacity(si, 16, 0), si := Chr(1) 
DllCall( "gdiplus\GdiplusStartup", UIntP,pToken, UInt,&si, UInt,0 ) 

DllCall( "gdiplus\GdipCreateBitmapFromStream", UInt,pStream, UIntP,pBitmap ) 
DllCall( "gdiplus\GdipCreateHBITMAPFromBitmap", UInt,pBitmap, UIntP,hBitmap, UInt,0 ) 

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap,, ahk_id %Pic1% 
Gui, Show,, Wallpaper by SoggyDog

DllCall( "gdiplus\GdipDisposeImage", UInt,pBitmap ) 
DllCall( "gdiplus\GdiplusShutdown", UInt,pToken ) 
DllCall( NumGet(NumGet(1*pStream)+8 ), UInt,pStream ) 
Return         

GuiClose:
GuiEscape:
 ExitApp
Return
Referencing a Laszlo LIB: :wink:

; See my post in Laszlo's Bit Wizardry
; http://www.autohotkey.com/forum/viewtopic.php?p=135550#135550

FileGetSize, dataSz , %A_AhkPath% 
FileRead   , bin    , %A_AhkPath% 
VarSetCapacity( hex, dataSz*2+1 ) 

hModule   := DllCall( "GetModuleHandle", Str, A_ScriptFullPath ) 
hResource := DllCall("FindResource", UInt,hModule, UInt,[color=red]777[/color], UInt,10 )
hResData  := DllCall("LoadResource", UInt,0, UInt,hResource )
Bin2Hex   := DllCall("LockResource", "UInt", hResData ) 

DllCall(Bin2Hex, "uint",&hex, "uint",&bin, "uint",dataSz, "cdecl" ) 
VarSetCapacity( hex, -1 )
MsgBox, %hex%
Dear Mr.Chris, :)

This is my Wish.

I request a change in behaviour of ahk2exe.exe :
Within Windows Explorer, when I select Compile Script from the context menu, if AutoHotkeySC.bin exists on the same folder as the script, then that version should be used for compilation.

This is to facilitate multiple projects that require seperate internal resources.

Thanks. :)

 

 

how to play MIDI or MP3 ,in compiled Script ?