Jump to content

[SOLVED] Adding ICO,BMP or JPG in EXE


Recommended Posts

Hi community

This is months am on this problem, I found tricks, but time is to solve it :)

I am intending to add some extra files (in this case, image files) into the output EXE, I tried allmost every piece of code thereby and even more tried using <ResourcesEX.au3> UDF

Nothing to do, each EXE compiled never display the "theorically embeded files"

Note :

Am compiling using SciteAutoIT 4.12

My AutoIT install is 3.3.14.5

#AutoIt3Wrapper_Icon=Offline.ico
#AutoIt3Wrapper_Res_Icon_Add=Offline.ico

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Test",150,50)

; This doesn't work
Global $butIcon = GUICtrlCreateButton("",10,10,20,20)
GUICtrlSetImage(-1, "Offline.ico", 0)

; This works
Global $picIcon = GUICtrlCreateIcon("Offline.ico",0,100,10,16,16)
GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

So, any suggestion to get my goal would be highly appreciated

Regards

 

Edited by Ebola57
Link to comment
Share on other sites

The file "Offline.ico" needs to be in the same folder as your script, whether the script is compiled or not. Looking at the code you have it seems that when compiling it can add the "Offline.ico" to be the icon used by the EXE file. But that does nothing for the button. Typically, here's what I do:

1. I use a function to create a random folder within the user's temp folder:

; #FUNCTION# ====================================================================================================================
; Name ..........: __RandomTempFolder
; Description ...: Creates a random temp folder to use while this is running in order to store any files needed and included in
;                  the script run
; Syntax ........: __RandomTempFolder()
; Parameters ....: None
; Return values .: None
; ===============================================================================================================================
Func __RandomTempFolder()
  Local $theFolderName, $i, $theRandomCharacter
  $theFolderName = "\~"
  For $i = 1 To 10
    $theRandomCharacter = Random(97, 122, 1)
    $theFolderName = $theFolderName & Chr($theRandomCharacter)
  Next
  Return $theFolderName
EndFunc   ;==>__RandomTempFolder

2. I use a variable within the script and set it to the path of the newly created temp folder:

$theSource = @TempDir & __RandomTempFolder()

3. I then use an initializing function to place all of my needed images in the newly created folder (this only shows .ICO icon files, but they can be any file type that your script might need):

; #FUNCTION# ====================================================================================================================
; Name ..........: __Initialize
; Description ...: Makes sure that images and support files exist for this wizard
; Syntax ........: __Initialize()
; Parameters ....: None
; Return values .: None
; ===============================================================================================================================
Func __Initialize()
  If FileExists($theSource) Then
    DirRemove($theSource)
  EndIf
  DirCreate($theSource)
  FileInstall("{the exact path to the image file}\error.ico", $theSource & "\error.ico", 1)
  FileInstall("{the exact path to the image file}\info.ico", $theSource & "\info.ico", 1)
  FileInstall("{the exact path to the image file}\question.ico", $theSource & "\question.ico", 1)
  FileInstall("{the exact path to the image file}\warning.ico", $theSource & "\warning.ico", 1)
EndFunc

4. Finally, when I set the button with an image on it, I use the path to the image file (I typically use variables to place the buttons and set the size of the buttons so that if I need to make the overall window wider or taller, the buttons adjust accordingly):

$btnMsgOK = GUICtrlCreateButton("", {button left}, {button top}, {button width}, {button height}, $BS_ICON)
GUICtrlSetImage(-1, $theSource & "\ok.ico")
GUICtrlSetTip(-1, "OK", "", 0, 1)

One more thing: I always, always, always delete the random temp folder after closing the window at the end of a script run. Windows doesn't clean up after itself, but that doesn't mean my scripts shouldn't.

Who lied and told you life would EVER be fair?

Link to comment
Share on other sites

If you want to use ressources included in the .exe, I would strongly recommend that you use RessourceEX UDF, unless you want to reinvent the wheel.  The example provided show you how to add different resources.  But if you only need icon to a button, you can do it like this :

#AutoIt3Wrapper_Res_Icon_Add=%AUTOITDIR%\Examples\Helpfile\Extras\Soccer.ico

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

Local Const $iHeight = 450, $iWidth = 470
$g_hGUI = GUICreate('Resources Example', $iWidth, $iHeight, Default, Default, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))

Local $iBall = GUICtrlCreateButton('Click', $iWidth - 168, 72, 68, 71, $BS_ICON)
GUICtrlSetImage($iBall, @AutoItExe, '201')   ; Icon resources added with #AutoIt3Wrapper_Res_Icon_Add, can be directly used without the UDF.

Global $picIcon = GUICtrlCreateIcon(@AutoItExe,201,100,10,32,32)

GUISetState(@SW_SHOW, $g_hGUI)

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop

  EndSwitch
WEnd

You will need to compile the script first...

Edited by Nine
Link to comment
Share on other sites

Another way to this is to embed the resource icon/image as Base64 code directly in the script code, and using this from @UEZ

I use these functions (I not 100%, but quit sure I got them from @UEZ also) to add images to either button or to a pic control.

; Set gui button images using Base64 and memwrite
Func SetBtnImg($hButton, $dString)
    Local $Bmp_Button = _GDIPlus_BitmapCreateFromMemory($dString, True)
    _WinAPI_DeleteObject(_SendMessage($hButton, $BM_SETIMAGE, $IMAGE_BITMAP, $Bmp_Button))
    _WinAPI_UpdateWindow($hButton)
    _WinAPI_DeleteObject($Bmp_Button)
EndFunc   ;==>SetBtnImg

;Set gui images using Base64 and memwrite
Func SetImg($idImg, $dString)
    Local $hBmp = _GDIPlus_BitmapCreateFromMemory($dString, True) ;to load an image from the net
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idImg, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp))
EndFunc   ;==>SetImg

 

Only drawback is if the images is updated, then you need to regenerate the Base64 code

Cheers
/Rex

Edited by Rex
Link to comment
Share on other sites

16 hours ago, benched42 said:

The file "Offline.ico" needs to be in the same folder as your script, whether the script is compiled or not. Looking at the code you have it seems that when compiling it can add the "Offline.ico" to be the icon used by the EXE file. But that does nothing for the button. Typically, here's what I do:

1. I use a function to create a random folder within the user's temp folder:

[...]

After closing the window at the end of a script run. Windows doesn't clean up after itself, but that doesn't mean my scripts shouldn't.

Thanks for this answer but my main goal is having a stand alone EXE without having to install any file

 

16 hours ago, Nine said:

If you want to use ressources included in the .exe, I would strongly recommend that you use RessourceEX UDF, unless you want to reinvent the wheel.  The example provided show you how to add different resources.  But if you only need icon to a button, you can do it like this :

You will need to compile the script first...

This is the code line that seems I was lacking. Going to try, Many thanks

GUICtrlSetImage($iBall, @AutoItExe, '201')   ; Icon resources added with #AutoIt3Wrapper_Res_Icon_Add, can be directly used without the UDF.
Global $picIcon = GUICtrlCreateIcon(@AutoItExe,201,100,10,32,32)

 

14 hours ago, Rex said:

Another way to this is to embed the resource icon/image as Base64 code directly in the script code, and using this from @UEZ

I[...]

/Rex

Will try if ResourcesEX fails, Thanks for alternate solution

Edited by Ebola57
Link to comment
Share on other sites

16 hours ago, Nine said:

If you want to use ressources included in the .exe, I would strongly recommend that you use RessourceEX UDF, unless you want to reinvent the wheel.  The example provided show you how to add different resources.  But if you only need icon to a button, you can do it like this :

@AutoItExe

You will need to compile the script first...

That maccro was the key, works flalessly now

Grateful thanks

Link to comment
Share on other sites

17 hours ago, Nine said:

If you want to use ressources included in the .exe, I would strongly recommend that you use RessourceEX UDF, unless you want to reinvent the wheel.  The example provided show you how to add different resources.  But if you only need icon to a button, you can do it like this :

#AutoIt3Wrapper_Res_Icon_Add=%AUTOITDIR%\Examples\Helpfile\Extras\Soccer.ico

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

Local Const $iHeight = 450, $iWidth = 470
$g_hGUI = GUICreate('Resources Example', $iWidth, $iHeight, Default, Default, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))

Local $iBall = GUICtrlCreateButton('Click', $iWidth - 168, 72, 68, 71, $BS_ICON)
GUICtrlSetImage($iBall, @AutoItExe, '201')   ; Icon resources added with #AutoIt3Wrapper_Res_Icon_Add, can be directly used without the UDF.

Global $picIcon = GUICtrlCreateIcon(@AutoItExe,201,100,10,32,32)

GUISetState(@SW_SHOW, $g_hGUI)

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop

  EndSwitch
WEnd

You will need to compile the script first...

Last but not least, that method rox for one icon icon, but to adress others as I need to ad seeveral icons in EXE ?

Link to comment
Share on other sites

8 minutes ago, Ebola57 said:

Last but not least, that method rox for one icon icon, but to adress others as I need to ad seeveral icons in EXE ?

It starts at 201, just add 1 for each icon (like 2nd is 202, 3rd is 203, etc...)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...