Sign in to follow this  
Followers 0
Kip

How to create a static image control with user32.dll

9 posts in this topic

Hi,

I'm trying to create a static control (already done that) with an image. (can't get that working)

This is what I have now:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
#include <UDFGlobalID.au3>
#include <SendMessage.au3>


$Gui = GUICreate("bla")

GUISetState()

_Image_Create($Gui, @ScriptDir&"\PageTwoItem.bmp", 10, 10, 55, 55)

While 1
    
WEnd

Func _Image_Create($hWnd, $sPath, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)
    
    $nCtrlID = _UDF_GetNextGlobalID($hWnd)
    $hBitmap = _WinAPI_LoadImage(0, $sPath, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
    
    $hImage = _WinApi_CreateWindowEx($iExStyle, "Static", $hBitmap, _
        $WS_VISIBLE+$WS_CHILD+$SS_NOTIFY+$SS_BITMAP+$iStyle, _
        $iX, $iY, $iWidth, $iHeight, _
        $hWnd, $nCtrlID, 0, 0)
    
    
    $STM_SETIMAGE = 0x172
    
    _SendMessage($hImage,$STM_SETIMAGE,$IMAGE_BITMAP,$hBitmap)
    
    return $hImage

EndFunc
But that doesn't work... (like I already said)

Share this post


Link to post
Share on other sites



AutoIt 3.2.10 syntax:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
;~ #include <UDFGlobalID.au3>
;~ #include <SendMessage.au3>

$Gui = GUICreate("bla", 400,300)
GUISetState()

_Image_Create($Gui, @ScriptDir&"\PageTwoItem.bmp", 10, 10, 55, 55)

While 1
    Sleep(100)
WEnd

Func _Image_Create($hWnd, $sPath, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)
        $CtrlID = GUICtrlCreateLabel('',$iX, $iY, $iWidth, $iHeight)
    $hBitmap = _WinAPI_LoadImage(0, $sPath, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
    _SetBitmapToCtrl($CtrlId, $hBitmap)

EndFunc

Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local Const $SS_BITMAP = 0xE
    Local Const $GWL_STYLE = -16

    Local $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(1, 0, 0)
        ; set SS_BITMAP style to control
    Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE)
    If @error Then Return SetError(2, 0, 0)
    DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle[0], $SS_BITMAP))
    If @error Then Return SetError(3, 0, 0)
        Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
    If @error Then Return SetError(4, 0, 0)
    If $oldBmp[0] <> 0 Then _WinAPI_DeleteObject($oldBmp[0])
    Return 1
EndFunc

_SetBitmapToCtrl() used from my resources UDF

http://www.autoitscript.com/forum/index.ph...st&p=386541

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

That is a normal label. I really need one created with _CreateWindowEx.

$nCtrlID = _UDF_GetNextGlobalID($hWnd)
    $hBitmap = _WinAPI_LoadImage(0, $sPath, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
    
    $hImage = _WinApi_CreateWindowEx($iExStyle, "Static", $hBitmap, _
        $WM_PAINT+$WS_VISIBLE+$WS_CHILD+$SS_NOTIFY+$SS_BITMAP+$iStyle, _
        $iX, $iY, $iWidth, $iHeight, _
        $hWnd, $nCtrlID, 0, 0)
    
    
    $STM_SETIMAGE = 0x172
    
    _SetBitmapToCtrl($hImage, $hBitmap)

This, again, doesn't work.

Edited by Kip

Share this post


Link to post
Share on other sites

I'm trying to create a label and image control created with CreateWindowEx, and I already have the label:

Func _GUICtrlLabel_Create($hWnd, $sText, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)
    
    $nCtrlID = _UDF_GetNextGlobalID($hWnd)
    
    $hLabel = _WinApi_CreateWindowEx($iExStyle, "Static", $sText, _
        $WS_VISIBLE+$WS_CHILD+$SS_NOTIFY+$iStyle, _
        $iX, $iY, $iWidth, $iHeight, _
        $hWnd, $nCtrlID, 0, 0)
    
    _WinAPI_SetFont($hLabel, _WinAPI_GetStockObject($DEFAULT_GUI_FONT))
    
    return $hLabel

EndFunc

Share this post


Link to post
Share on other sites

GuiCtrlCreatePic


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

You have to chagen setimage to accept Handles and not just CtrlIDs :)

CODE
#include <WindowsConstants.au3>

#include <GUIConstantsEx.au3>

#include <Constants.au3>

#include <StaticConstants.au3>

#include <WinAPI.au3>

;~ #include <UDFGlobalID.au3>

;~ #include <SendMessage.au3>

$Gui = GUICreate("bla", 400,300)

GUISetState()

_Image_Create($Gui, @SystemDir & "\oemlogo.bmp", 10, 10, 55, 55)

While 1

Sleep(100)

WEnd

Func _Image_Create($hWnd, $sPath, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)

$CtrlID = _GUICtrlLabel_Create($hWnd,'',$iX, $iY, $iWidth, $iHeight)

$hBitmap = _WinAPI_LoadImage(0, $sPath, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)

_SetBitmapToCtrl($CtrlId, $hBitmap)

EndFunc

Func _GUICtrlLabel_Create($hWnd, $sText, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)

;~ $nCtrlID = _UDF_GetNextGlobalID($hWnd)

$hLabel = _WinApi_CreateWindowEx($iExStyle, "Static", $sText, _

$WS_VISIBLE+$WS_CHILD+$SS_NOTIFY+$iStyle, _

$iX, $iY, $iWidth, $iHeight, _

$hWnd);, $nCtrlID, 0, 0)

;~ _WinAPI_SetFont($hLabel, _WinAPI_GetStockObject($DEFAULT_GUI_FONT))

return $hLabel

EndFunc

Func _SetBitmapToCtrl($CtrlId, $hBitmap)

Local Const $STM_SETIMAGE = 0x0172

Local Const $IMAGE_BITMAP = 0

Local Const $SS_BITMAP = 0xE

Local Const $GWL_STYLE = -16

Local $hWnd = GUICtrlGetHandle($CtrlId)

If IsHWnd($CtrlID) And WinExists($CtrlID) Then $hWnd = $CtrlID

If $hWnd = 0 Then Return SetError(1, 0, 0)

; set SS_BITMAP style to control

Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE)

If @error Then Return SetError(2, 0, 0)

DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle[0], $SS_BITMAP))

If @error Then Return SetError(3, 0, 0)

Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)

If @error Then Return SetError(4, 0, 0)

If $oldBmp[0] <> 0 Then _WinAPI_DeleteObject($oldBmp[0])

Return 1

EndFunc

And btw, why do you use GetNextGlobalID? THe parameter after $hwnd is for a Menuhandle, not for an ID. The ID is genersted automatically :P


*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

#8 ·  Posted (edited)

GuiCtrlCreatePic

Have you ever read my topic? :) I'm not that dumb (yes, i'm serious :P )

@ProgAndy: Thanks, that works :P

And btw, why do you use GetNextGlobalID? THe parameter after $hwnd is for a Menuhandle, not for an ID. The ID is genersted automatically smile.gif

Because all the other _GUICtrl UDFs use that, it was kinda weird to me too.

I have this:

Func _Image_Create($hWnd, $sPath, $iX, $iY, $iWidth, $iHeight, $iStyle=0, $iExStyle=0)
     
     Local Const $STM_SETIMAGE = 0x0172
     Local Const $SS_BITMAP = 0xE
     
     $hImage = _WinApi_CreateWindowEx($iExStyle, "Static", "", $WS_VISIBLE+$WS_CHILD+$SS_NOTIFY+$SS_BITMAP+$iStyle, $iX, $iY, $iWidth, $iHeight, $hWnd)
     $hBitmap = _WinAPI_LoadImage(0, $sPath, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
     
     Local $hOldBitmap = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hImage, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
     If $hOldBitmap[0] <> 0 Then _WinAPI_DeleteObject($hOldBitmap[0])
 
 EndFunc
But when I comment out the 4th line (where $SS_BITMAP is declared) it doesn't work anymore.

So, is the $SS_BITMAP of StaticConstants.au3 wrong?

Edit: From StaticConstants.au3: "Global Const $SS_BITMAP = 15" = Dec(0xE)

Edited by Kip

Share this post


Link to post
Share on other sites

The ID is genersted automatically :)

No it's not. Unless you are talking about controls made with native GuiCtrlCreate... functions, but topic starter is way too smart to use these.

"be smart, drink your wine"

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