Sign in to follow this  
Followers 0
AustrianOak

Putting icons into an .exe

9 posts in this topic

My script uses icon files and if I didn't want other users of the program to be able to access them directly, could I just put them into the .exe.

I vaguely remember there should be some function to do this...

Share this post


Link to post
Share on other sites



#Region AutoIt3Wrapper directives section

#AutoIt3Wrapper_Icon=audio_cd.ico

#EndRegion AutoIt3Wrapper directives section

Share this post


Link to post
Share on other sites

I don't think that's what I was expecting.

Something like an Install function that puts whatever icons I want into the .exe and I can use them anywhere on the gui.

Share this post


Link to post
Share on other sites

oh ok.

then use this with resource .au3

i havent tested with ICO files though.-.?

#Region AutoIt3Wrapper directives section

;;#AutoIt3Wrapper_Res_File_Add=test_1.txt, rcdata, TEST_TXT_1 ; Filename,Section,ResName

;#AutoIt3Wrapper_Res_File_Add=image1.bmp, bitmap, TEST_BMP_1

;#AutoIt3Wrapper_Res_File_Add=image2.bmp, bitmap, TEST_BMP_2

;;#AutoIt3Wrapper_Res_File_Add=binary1.dat, rcdata, TEST_BIN_1

;;#AutoIt3Wrapper_Res_File_Add=C:\WINNT\Media\tada.wav, sound, TEST_WAV_1

; turn OFF Compression in beginning or it wont work ---

#AutoIt3Wrapper_UseUpx=n

;; add icon

#AutoIt3Wrapper_Icon=earth.ico

#AutoIt3Wrapper_Run_After=c:\ResHacker.exe -add %out%, %out%, image1.bmp , 2, TEST_BMP_1, 0

#AutoIt3Wrapper_Run_After=c:\ResHacker.exe -add %out%, %out%, image2.bmp , 2, TEST_BMP_2, 0

#AutoIt3Wrapper_Run_After=c:\ResHacker.exe -add %out%, %out%, test_1.gif, 23, TEST_GIF_1, 0

; now compress resources with UPX

#AutoIt3Wrapper_Run_After=c:\upx.exe --compress-resources=1 "%out%"

#EndRegion

#include "resources.au3"

$gui = GUICreate("Data from resources example",820,400)

$pic1 = GUICtrlCreatePic("",0,0,400,300)

$pic2 = GUICtrlCreatePic("",400,0,400,300)

$label = GUICtrlCreateLabel("test",20,320,800,100)

GUISetState(@SW_SHOW)

; get string from resource

;$string = _ResourceGetAsString("TEST_TXT_1")

;GUICtrlSetData($label, $string)

; set image to picture control from resource bitmap

_ResourceSetImageToCtrl($pic1, "TEST_BMP_1", $RT_BITMAP)

; set image to picture control from resource bitmap

_ResourceSetImageToCtrl($pic2, "TEST_GIF_1", $RT_BITMAP)

; save binary data from resource to file

_ResourceSaveToFile(@ScriptDir & "\binary_data.dat", "TEST_BIN_1")

; save binary data from resource to file (create not existing directory)

_ResourceSaveToFile("teest__.dat", "TEST_BIN_1", $RT_RCDATA, 0, 1)

; play WAV from resource (sync/async)

_ResourcePlaySound("TEST_WAV_1")

_ResourcePlaySound("TEST_WAV_1", $SND_ASYNC)

While 1

If GUIGetMsg() = -3 Then Exit

WEnd

resource .au3

#include-once

Global Const $RT_CURSOR = 1
Global Const $RT_BITMAP = 2
Global Const $RT_ICON = 3
Global Const $RT_MENU = 4
Global Const $RT_DIALOG = 5
Global Const $RT_STRING = 6
Global Const $RT_FONTDIR = 7
Global Const $RT_FONT = 8
Global Const $RT_ACCELERATORS = 9
Global Const $RT_RCDATA = 10
Global Const $RT_MESSAGETABLE = 11
Global Const $RT_GROUP_CURSOR = 12
Global Const $RT_GROUP_ICON = 14
Global Const $RT_VERSION = 16

Global Const $RT_ANICURSOR = 21
Global Const $RT_ANIICON = 22
Global Const $RT_HTML = 23
Global Const $RT_MANIFEST = 24

Global Const $SND_RESOURCE = 0x00040004
Global Const $SND_SYNC = 0x0
Global Const $SND_ASYNC = 0x1
Global Const $SND_LOOP = 0x8
Global Const $SND_NOSTOP = 0x10
Global Const $SND_NOWAIT = 0x2000
Global Const $SND_PURGE = 0x40

Func _ResourceGet($ResName, $ResType = 10, $ResLang = 0) ; $RT_RCDATA = 10
    Local Const $IMAGE_BITMAP = 0
    Local $hInstance, $hBitmap, $InfoBlock, $GlobalMemoryBlock, $MemoryPointer, $ResSize
    
    $hInstance = DllCall("kernel32.dll", "int", "GetModuleHandleA", "int", 0)
    $hInstance = $hInstance[0]

    If $ResType = $RT_BITMAP Then
        $hBitmap = DllCall("user32.dll", "hwnd", "LoadImage", "hwnd", $hInstance, "str", $ResName, _
                "int", $IMAGE_BITMAP, "int", 0, "int", 0, "int", 0)
        If @error Then Return SetError(1, 0, 0)
        $hBitmap = $hBitmap[0]
        Return $hBitmap ; returns handle to Bitmap
    EndIf

    If $ResLang <> 0 Then
        $InfoBlock = DllCall("kernel32.dll", "int", "FindResourceExA", "int", $hInstance, "str", $ResName, "long", $ResType, "short", $ResLang)
    Else
        $InfoBlock = DllCall("kernel32.dll", "int", "FindResourceA", "int", $hInstance, "str", $ResName, "long", $ResType)
    EndIf
    
    If @error Then Return SetError(2, 0, 0)
    $InfoBlock = $InfoBlock[0]
    If $InfoBlock = 0 Then Return SetError(3, 0, 0)
    
    $ResSize = DllCall("kernel32.dll", "dword", "SizeofResource", "int", $hInstance, "int", $InfoBlock)
    If @error Then Return SetError(4, 0, 0)
    $ResSize = $ResSize[0]
    If $ResSize = 0 Then Return SetError(5, 0, 0)
    
    $GlobalMemoryBlock = DllCall("kernel32.dll", "int", "LoadResource", "int", $hInstance, "int", $InfoBlock)
    If @error Then Return SetError(6, 0, 0)
    $GlobalMemoryBlock = $GlobalMemoryBlock[0]
    If $GlobalMemoryBlock = 0 Then Return SetError(7, 0, 0)
    
    $MemoryPointer = DllCall("kernel32.dll", "int", "LockResource", "int", $GlobalMemoryBlock)
    If @error Then Return SetError(8, 0, 0)
    $MemoryPointer = $MemoryPointer[0]
    If $MemoryPointer = 0 Then Return SetError(9, 0, 0)
    
    SetExtended($ResSize)
    Return $MemoryPointer
EndFunc

Func _ResourceGetAsString($ResName, $ResType = 10, $ResLang = 0) ; $RT_RCDATA = 10
    Local $ResPointer, $ResSize, $struct

    $ResPointer = _ResourceGet($ResName, $ResType, $ResLang)
    If @error Then
        SetError(1, 0, 0)
        Return ''
    EndIf
    $ResSize = @extended
    $struct = DllStructCreate("char[" & $ResSize & "]", $ResPointer)
    Return DllStructGetData($struct, 1) ; returns string
EndFunc

Func _ResourceGetAsBytes($ResName, $ResType = 10, $ResLang = 0) ; $RT_RCDATA = 10
    Local $ResPointer, $ResSize

    $ResPointer = _ResourceGet($ResName, $ResType, $ResLang)
    If @error Then Return SetError(1, 0, 0)
    $ResSize = @extended
    Return DllStructCreate("byte[" & $ResSize & "]", $ResPointer) ; returns struct with bytes
EndFunc

Func _ResourceSaveToFile($FileName, $ResName, $ResType = 10, $ResLang = 0, $CreatePath = 0) ; $RT_RCDATA = 10
    Local $ResStruct, $ResSize, $FileHandle

    $ResStruct = _ResourceGetAsBytes($ResName, $ResType, $ResLang)
    If @error Then Return SetError(1, 0, 0)
    $ResSize = DllStructGetSize($ResStruct)
    
    If $CreatePath Then $CreatePath = 8 ; mode 8 = Create directory structure if it doesn't exist
    $FileHandle = FileOpen($FileName, 2+16+$CreatePath)
    If @error Then Return SetError(2, 0, 0)
    FileWrite($FileHandle, DllStructGetData($ResStruct, 1))
    If @error Then Return SetError(3, 0, 0)
    FileClose($FileHandle)
    If @error Then Return SetError(4, 0, 0)

    Return $ResSize
EndFunc

Func _ResourceSetImageToCtrl($CtrlId, $ResName, $ResType = 10) ; $RT_RCDATA = 10
    Local $ResData = _ResourceGet($ResName, $ResType)
    If @error Then Return SetError(1, 0, 0)
    
    If $ResType = $RT_BITMAP Then
        _SetBitmapToCtrl($CtrlId, $ResData)
        If @error Then Return SetError(2, 0, 0)
        Return 1
    EndIf
    
    ; for other types then BITMAP not implemented yet (must be used GDI+) ...
    ; ...
    Return SetError(-1, 0, 0)
EndFunc

; internal helper function
Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0

    Local $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(1, 0, 0)
    DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
    If @error Then Return SetError(2, 0, 0)
    Return 1
EndFunc

; thanks Larry
; MSDN: http://msdn2.microsoft.com/en-us/library/ms712879.aspx
; default flag is $SND_SYNC = 0
Func _ResourcePlaySound($ResName, $Flag = 0)
    Local $ret = DllCall("winmm.dll", "int", "PlaySound", "str", $ResName, "hwnd", 0, "int", BitOr($SND_RESOURCE,$Flag))
    If @error Then Return SetError(1, 0, 0)
    Return $ret[0]
EndFunc

Share this post


Link to post
Share on other sites

To add icons to an exe using autoit code you would need to use/translate the 3 win api functions:

BeginUpdateResource Function

UpdateResource Function

EndUpdateResource Function

Read about these functions here: MSDN

I'm currently using these functions in autoit, but I'm not ready to post the code as of yet.

Due to I haven't written the functions to be modular or user friendly yet.

The things I need to work on is the error handling mainly.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

It's more simple than above posts want to do :-)

#AutoIt3Wrapper_Icon=base_app_icon.ico
#AutoIt3Wrapper_Res_Icon_Add=Icon1.ico
#AutoIt3Wrapper_Res_Icon_Add=Icon2.ico
#AutoIt3Wrapper_Res_Icon_Add=Icon3.ico

; examples of use:
TraySetIcon(@ScriptFullPath, -4); Icon1.ico

            GUICtrlSetImage(-1, @ScriptFullPath, -4);; Icon1.ico

    $LViewImageList = ImageList_Create(16, 16, BitOR($ILC_MASK, $ILC_COLOR32), 2, 0)
$stIcon = DllStructCreate("int")
ExtractIconEx(@ScriptFullPath, 4, 0, DllStructGetPtr($stIcon), 1); sort_asc.ico
ImageList_AddIcon($LViewImageList, DllStructGetData($stIcon, 1))
ExtractIconEx(@ScriptFullPath, 5, 0, DllStructGetPtr($stIcon), 1); sort_desc.ico
ImageList_AddIcon($LViewImageList, DllStructGetData($stIcon, 1))
DestroyIcon(DllStructGetData($stIcon, 1))
$stIcon = 0
$Header = GUICtrlSendMsg($ListView1, $LVM_GETHEADER, 0, 0)
DllCall("user32.dll", "int", "SendMessage", "hwnd", $Header, "int", $HDM_SETIMAGELIST, "int", 0, "hwnd", $LViewImageList)
Edited by Zedna

Share this post


Link to post
Share on other sites

I can't get "_ResourceSetImageToCtrl" to work.

Can anyone help?

Thanks.

Share this post


Link to post
Share on other sites

For Icons, this is NOT needed muttley Just use

GUICtrlSetImage(-1, @ScriptFullPath, -4);; Icon1.ico

like for normal Icons


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0