Jump to content

Display images centered at the Picture control


AndreyS
 Share

Recommended Posts

Please show some of the code you have tried to give us a bit of a headstart, my crystal ball is a bit foggy right now.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

No problems!

...
$avatar = GUICtrlCreatePic("", 25, 140, 130, 130, $WS_BORDER)
...
Func ChangeAvatar()
    GUICtrlDelete($avatar2)
    $avatar = GUICtrlCreatePic("...\avatar.jpg", 25, 140, 0, 0, BitOR($SS_NOTIFY,$SS_RIGHTJUST))
EndFunc   ;==>ChangeAvatar
...

Pictures are loaded (avatar.jpg) size 60x90, 100x70 px and other...

Link to comment
Share on other sites

First of all simpler way of changing image:

ChangeAvatar("C:\avatar.jpg")

Func ChangeAvatar($image)
    GUICtrlSetImage($avatar, $image)
EndFunc   ;==>ChangeAvatar

As about centering/shrinking images I'm not sure but I remember somebody on this forum showed ideas about that already. Try to search for it.

Edited by Zedna
Link to comment
Share on other sites

#Include <GDIPlus.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

GUICreate('MyGUI', 400, 400)
$Pic = GUICtrlCreatePic('', 0, 0, 400, 400)
_GUICtrlSetImage($Pic, RegRead('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'InstallDir') & '\Examples\GUI\logo4.gif', 0)
;_GUICtrlSetImage($Pic, RegRead('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'InstallDir') & '\Examples\GUI\logo4.gif')
GUISetState()

Do
Until GUIGetMsg() = -3

Func _GUICtrlSetImage($hWnd, $sImage, $fScale = 1)

    Local Const $STM_SETIMAGE = 0x0172
    Local Const $STM_GETIMAGE = 0x0173

    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If $hWnd = 0 Then
            Return 0
        EndIf
    EndIf

    Local $Res, $tRect, $hImage, $hBitmap, $hBmp, $hObj, $Width, $Height, $hDC, $hDestDC, $hSrcDC

    _GDIPlus_Startup()
    $hImage = _GDIPlus_BitmapCreateFromFile($sImage)
    $Width = _GDIPlus_ImageGetWidth($hImage)
    $Height = _GDIPlus_ImageGetHeight($hImage)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    If Not $hBitmap Then
        Return 0
    EndIf

    $tRect = _WinAPI_GetClientRect($hWnd)
    $W = DllStructGetData($tRect, 3) - DllStructGetData($tRect, 1)
    $H = DllStructGetData($tRect, 4) - DllStructGetData($tRect, 2)
    $hDC = _WinAPI_GetDC($hWnd)
    $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $W, $H)
    _WinAPI_SelectObject($hDestDC, $hBmp)
    $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
    _WinAPI_SelectObject($hSrcDC, $hBitmap)
    If $fScale Then
        $Res = DllCall('gdi32.dll', 'int', 'StretchBlt', 'hwnd', $hDestDC, 'int', 0, 'int', 0, 'int', $W, 'int', $H, 'hwnd', $hSrcDC, 'int', 0, 'int', 0, 'int', $Width, 'int', $Height, 'dword', $SRCCOPY)
    Else
        $Res = DllCall('gdi32.dll', 'int', 'BitBlt', 'hwnd', $hDestDC, 'int', 0, 'int', 0, 'int', $W, 'int', $H, 'hwnd', $hSrcDC, 'int', Round(($Width - $W) / 2), 'int', Round(($Height - $H) / 2), 'dword', $SRCCOPY)
    EndIf
    If (@error) Or (Not $Res[0]) Then
        $Res = 0
    Else
        $Res = 1
    EndIf

    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($hDestDC)
    _WinAPI_DeleteDC($hSrcDC)

    If Not $Res Then
        _WinAPI_DeleteObject($hBmp)
    Else
        _WinAPI_DeleteObject(_SendMessage($hWnd, $STM_SETIMAGE, 0, 0))
        _SendMessage($hWnd, $STM_SETIMAGE, 0, $hBmp)
        $hObj = _SendMessage($hWnd, $STM_GETIMAGE)
        If $hObj <> $hBmp Then
            _WinAPI_DeleteObject($hBmp)
        EndIf
    EndIf
    Return $Res
EndFunc   ;==>_GUICtrlSetImage

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