Jump to content

Loading (image) resource from file


Recommended Posts

Hi :D

does anybody already have a working DllCall() to load a resource from a file?

I got some function calls (LoadImage i.e. or FindResource) here but at the moment a little bit ugly :D

Maybe someone has some clear code lines.

Otherwise I have to clean up mine and will post it to Scripts/Scrapts.

I just use it to prepare the tristate-treeview (I converted my old c++code to some Autoit3 lines).

At the moment only LoadImage() works completely.

FindResource() works so far but after LoadResouce() and updating for instance a GUI-picture with the loaded bitmap the picture will not display the new bitmap handle.

Greets

Holger

Link to comment
Share on other sites

Holger,

I tried sometime back to modify Larry's version of LoadImage function to work for Loading images from resources. And I partially succeeded.

I don't know whether its of any use to you, but here is how it works:

I didn't know how to "get" a resource from a file on disk (like shell32.dll), instead this script can get a resource from "its own compiled executable file".

To add resources, I simply used resource hacker to add bmp images to AUTOITSC.BIN and when you compile the script, those bmp images get added to the compiled executable.

The function that loads the resource images from the executable is:

Func LoadImageFromResource(ByRef $hWin, ByRef $nID, $sRes)
    Local Const $GWL_HINSTANCE = -6
    Local Const $IMAGE_BITMAP = 0
    
    Local $ret[4], $hInst, $hBmp, $phWnd, $dc, $dc1, $old
    
    $hInst = DllCall("user32.dll", "int", "GetWindowLong", "hWnd", $hWin, "int", $GWL_HINSTANCE)

    $hBmp = DllCall("user32.dll", "hwnd", "LoadImage", "hwnd", $hInst[0], "str", $sRes, _
                "int", $IMAGE_BITMAP, "int", 0, "int", 0, "int", 0)
        
    $phWnd = DLLCall("user32.dll","hwnd","GetDlgItem","hwnd",$hWin,"int",$nID)

    $dc = DllCall("user32.dll","int","GetDC","hwnd",$phWnd[0])
    
    $dc1 = DLLCall("gdi32.dll","int","CreateCompatibleDC","int",$dc[0])

    $old = DLLCall("gdi32.dll","hwnd","SelectObject","int",$dc1[0],"hwnd",$hBmp[0])
    
    $ret[0] = $dc[0]
    $ret[1] = $dc1[0]
    $ret[2] = $hBmp[0]
    $ret[3] = $old[0]
    
    Return $ret
EndFunc

where $hWnd = handle of the AutoIt Gui

$nID = handle of the control which displays the picture (like a picture box).

$sRes = name of the resource in the executable.

Download from here for the complete working example:

http://www.mytempdir.com/784085

This zip file has a folder "RES" which has a copy of AUTOITSC.BIN modified to add a resource image called "BAR". This image gets loaded by the script ONLY WHEN IT IS COMPILED.

Right-Click on the LoadImage.au3 script and choose "Compile with Options". This ensures that the modified version of AUTOITSC.BIN is used to compile the script.

Here's how it would look if you did everything correctly:

Posted Image

Sorry, for this obsure example, but this is the only way its working. If you can find a way to load resources even from static DLLs on disk, I would be very thankful for an example.

Hope this is useful.

Thanks,

Tonedeaf

Link to comment
Share on other sites

  • 7 months later...

Does anyone have a copy of this example as the link does not work anymore :whistle:

Or

I have embeded an image that i want using Resource Hacker into AUTOITSC.BIN

When i compile my script and load it into Resource Hacker the Image is included within the exe thats made.

The image is located at

IMAGE

|

HEADIMG

|

0 - 0 is the image and displays if i select it in Resource Hacker

I want the image to be displayed under

$Graphic1 = GUICtrlCreateGraphic(34, 10, 400, 70)oÝ÷ Ø   ݶ¢jbç^Åè¬q©eyÚ®¢Û^²Ø.ì^ªê-Jë¢kay¦è½êh²Ü"¶.µìZ^¶âØhÂ+!¢é]mçhx-+"«Þjëh×6$hWnd = "testgui"
$nID = $Graphic1
$sRes = "0"

LoadImageFromResource(ByRef $hWin, ByRef $nID, $sRes)

But this is not working the image is not displayed :)

Does anyone know what i should be putting to make the image load?

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...