Sign in to follow this  
Followers 0
Tomb

loading pictures (not icons) from a dll

9 posts in this topic

i have this .DLL i made. well actually i just found a .DLL on my pc and copied it and edited everything out. then i put in some bmp's as resources with resource hacker. how can i GuiCtrlCreatePic the images i put in my .DLL as resources? i was looking up DllCall and various other functions, and i see that GuiSetImage works for icons, but these are like bmps and jpgs. also i want to use GuiCtrlCreateImage if possible.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Look at my resources UDF  but consider it's only for AutoIt script's EXE not for external DLLs.

Take it as similar example only.

You must use LoadLibrary, LoadImage API functions.

Search forum for LoadImage I think you will find some more examples.

Edited by Zedna

Share this post


Link to post
Share on other sites

Look at my resources UDF  but consider it's only for AutoIt script's EXE not for external DLLs.

Take it as similar example only.

You must use LoadLibrary, LoadImage API functions.

Search forum for LoadImage I think you will find some more examples.

that looks complicated

is there really no way to just GuiCtrlCreatePic and set the target to a resource in my .DLL file?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

so should i assume its impossible?

i see that many non-autoit programs store and load images in dll's, maybe autoit cannot do something like this?

just loading images and probably sounds from a .dll

Edited by Tomb616

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

"Complicated" and "impossible" are relative. You were given answer, and I'd say that even if GUICtrlSetImage really does not work, a LoadLibrary+LoadImage+SendMessage isn't complicated at all. Even if it looks that way to you.

#include <GUIConstants.au3>
#include <WinAPI.au3>

Global Const $STM_SETIMAGE = 0x0172

$Form1 = GUICreate("", 400, 400)
$pic = GUICtrlCreatePic("", 0, 20, 400, 380)
GUICtrlSetState(-1, $GUI_DISABLE)
$b1 = GUICtrlCreateButton("Change Pic", 150, 0, 100, 20)
GUISetState(@SW_SHOW)
Global $i = 0, $hBmp = 0, $aBitmaps[46] = [130,131,133,134,135,136,137,138,140,141,142,143, 14351,14353,14354,14355,14356, 145,146,147,148,149,204,205,206,207,214,215,216,217,225,226,227,228,230,231,240,241,242,245,246,247, 309,310,369,390]
While 1
    Switch GuiGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $b1
            If $hBmp <> 0 Then _WinAPI_DeleteObject($hBmp)
            $hLib = _WinAPI_LoadLibrary('shell32.dll')
            $hBmp = _WinAPI_LoadImage($hLib, $aBitmaps[$i], $IMAGE_BITMAP, 0, 0, $LR_DEFAULTCOLOR)
            GUICtrlSendMsg($pic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp)
            _WinAPI_FreeLibrary($hLib)
            $i += 1
            If $i = UBound($aBitmaps)-1 Then $i = 0
    EndSwitch
WEnd
Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks for nice/simple example Siao!

Here is derived version in UDF way:

Func GUICtrlSetImageFromDLL($controlID, $filename, $imageIndex)

#include <GUIConstants.au3>
#include <WinAPI.au3>

$Form1 = GUICreate("", 400, 400)
$pic = GUICtrlCreatePic("", 0, 20, 400, 380)
GUICtrlSetState(-1, $GUI_DISABLE)
$b1 = GUICtrlCreateButton("Change Pic", 150, 0, 100, 20)
GUISetState(@SW_SHOW)
Global $i = 0, $aBitmaps[46] = [130,131,133,134,135,136,137,138,140,141,142,143, 14351,14353,14354,14355,14356, 145,146,147,148,149,204,205,206,207,214,215,216,217,225,226,227,228,230,231,240,241,242,245,246,247, 309,310,369,390]
While 1
    Switch GuiGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $b1
            GUICtrlSetImageFromDLL($pic, 'shell32.dll', $aBitmaps[$i])
            $i += 1
            If $i = UBound($aBitmaps)-1 Then $i = 0
    EndSwitch
WEnd

Func GUICtrlSetImageFromDLL($controlID, $filename, $imageIndex)
    Local Const $STM_SETIMAGE = 0x0172

    $hLib = _WinAPI_LoadLibrary($filename)
    $hBmp = _WinAPI_LoadImage($hLib, $imageIndex, $IMAGE_BITMAP, 0, 0, $LR_DEFAULTCOLOR)
    GUICtrlSendMsg($controlID, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp)
    _WinAPI_FreeLibrary($hLib)
    _WinAPI_DeleteObject($hBmp)
EndFunc
Edited by Zedna

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Problem with deleting bitmap object right away is that the pic control won't have it anymore if it ever needs to repaint itself (after the window is covered or deactivated). Ideally you want to delete it only when you don't need it, which is right before changing to another image.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Problem with deleting bitmap object right away is that the pic control won't have it anymore if it ever needs to repaint itself (after the window is covered or deactivated). Ideally you want to delete it only when you don't need it, which is right before changing to another image.

AAh. You are right.

Thanks for info.

I learned something new ;-)

So simplest solution for my UDF version is to don't do _WinAPI_DeleteObject($hBmp)

some resources will not be released but it's not fatal in this case.

EDIT:

Clear solution will be to get existing (old) bitmap handle at begining and release it.

I don't know if there some API for that maybe GetStockObject?

Edited by Zedna

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thanks for help. However, I found a problem. if I minimize the window, restoring it will cause the image to be gone (just gray box)... at least on my GUI

Edit: fixed it by not deleting, like you said. Thanks again.

Edited by sensalim

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