Jump to content

Recommended Posts

Posted (edited)

So I have made great progress this weekend with my project but im a little stuck so hoping some great minds can help a little.

So far this I what I have made , An GUI which loads an img which can be zoomed in an/out and scrolled around. Now I want to load smaller images on top of this main GUI/img Calculating the position on this was a bit tricky but I managed to solve that. But the img I load flickers like crazy everytime it gets reloaded which is quite often (on every scroll/zoom) 

I´m looking for a bit help to make this smaller image not flicker at all

below is my entire script (for testing purposes  

#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <Misc.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("^0", "_ResetZoom") ; ctr and 0
HotKeySet("^0", "_ResetZoom") ; ctr and 0
Global $sx, $z = 5, $sy, $mc2, $f, $scroll_x = 0, $scroll_y, $current_iPos_y, $RQGUI_POS[3] = [280, 325, 0]
Global $g_iIDC = -1, $g_iNewIDC = 0, $bg_c = "404040"
Global $aDim, $hImage = 0, $iMinScrollx, $iMinScrolly
Global $g_aArray = StringSplit("Hand|AppStarting|Arrow|Cross|Help|IBeam|Icon (obsolete)|No|" & _
        "Size (obsolete)|SizeAll|SizeNESW|SizeNS|SizeNWSE|SizeWE|UpArrow|Wait|None", "|", 2) ; The flag parameter is set to flag = 2 as we don't require the total count of the array.

; --- Create GUI ---
$hGUI = GUICreate('MyGUI', @DesktopWidth - 200, @DesktopHeight - 200, 100, 100, $WS_POPUP)
Global $GUI_Pos = WinGetPos($hGUI)
Global $iGUIWidth = $GUI_Pos[2]
Global $iGUIHeight = $GUI_Pos[3]
GUICtrlSetBkColor($hGUI, 0x000000)

GUISetState()
Global Const $dll = DllOpen("user32.dll")

;====================================================
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "testimg.jpg")

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hBuffer_Bmp = _GDIPlus_BitmapCreateFromGraphics($iGUIWidth, $iGUIHeight, $hGraphic)
Global Const $hContext = _GDIPlus_ImageGetGraphicsContext($hBuffer_Bmp)
_GDIPlus_GraphicsSetPixelOffsetMode($hContext, 2)
_GDIPlus_GraphicsSetCompositingQuality($hContext, 2)
_GDIPlus_GraphicsSetInterpolationMode($hContext, 5)

Global $hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $iGUIWidth / 2, $iGUIHeight / 2)
Global Const $hBmp = _GDIPlus_GraphicsCreateFromHWND($hGUI)

$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)


GUIRegisterMsg($WM_MOUSEWHEEL, "_Mousewheel")

; --- GUI Dimensions ---
Global $aDim = _GDIPlus_ImageGetDimension($hImage)
Global $iWidth = _GDIPlus_ImageGetWidth($hImage)
Global $iHeight = _GDIPlus_ImageGetHeight($hImage)
Global $fZoom = 1
Global $iMinZoom = 1
Global $iMaxZoom = 20
Global $imgwidth = $iWidth
Global $imgheight = $iHeight
Global $imgxDIFF = 0
Global $imgyDIFF = 0

Draw2Graphic()
_Loadimages()
Func _Loadimages()
    $base_x = 500
    $base_y = 300
    $start_x = -$iMinScrollx
    $start_y = -$iMinScrolly
    $ix = $start_x - $scroll_x ; pos 0 on img
    $iy = $start_y - $scroll_y ; pos 0 on img
    $xx = ($ix + $base_x) * $fZoom
    $yy = ($iy + $base_y) * $fZoom

    ConsoleWrite("$xx= " & $xx & @LF)
    ConsoleWrite("$yy= " & $yy & @LF)
    ConsoleWrite("scrolly= " & $scroll_y & @LF)
    ConsoleWrite("End_y= " & (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly & @LF)
    ConsoleWrite("$fZoom " & $fZoom & @LF)
    ; ,x:-1,y:3

    $base = 10
    $Pos_x = 3 * $base
    $hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")
    $x = _GDIPlus_ImageGetWidth($hImage_1)
    $y = _GDIPlus_ImageGetHeight($hImage_1)
    ;_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $Pos_x, 110, $x/3.5, $y/3.5)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $xx, $yy, 25, 25)
EndFunc   ;==>_Loadimages
Do
    $mc2 = MouseGetPos()
    If $g_iIDC = $g_iNewIDC Then
        $g_iIDC = -1
        GUISetCursor($g_iIDC)
    EndIf
    While _IsPressed("01", $dll)
        If $g_iNewIDC <> $g_iIDC Then
            $g_iIDC = $g_iNewIDC
            GUISetCursor($g_iIDC)
        EndIf
        GUICtrlSetCursor(-1, 4)
        $mc3 = MouseGetPos()
        If $mc2[0] <> $mc3[0] Or $mc2[1] <> $mc3[1] Then
            $scroll_x = ($sx + ($mc3[0] - $mc2[0]) / ($z ^ 2 / 16))
            $scroll_y = ($sy + ($mc3[1] - $mc2[1]) / ($z ^ 2 / 16))

            Draw2Graphic()
            $current_iPos_y = $scroll_y
        EndIf

        Sleep(20)
    WEnd
    $sx = $scroll_x
    $sy = $scroll_y
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_GDIPlus_GraphicsDispose($hGraphic)
DllClose($dll)

Func Draw2Graphic()
    _GDIPlus_GraphicsClear($hContext, "0xFF" & $bg_c)
    $iMinScrollx = (($imgxDIFF / 2)) / $fZoom
    $iMinScrolly = ($imgyDIFF) / $fZoom

    If $scroll_y <= -($iMinScrolly) Then $scroll_y = -($iMinScrolly)
    If $scroll_x <= -(($imgxDIFF / 2)) / $fZoom Then $scroll_x = -(($imgxDIFF / 2)) / $fZoom
    ;ConsoleWrite("$iMinScrolly= " & $iMinScrolly & @LF)
    If $scroll_y >= (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly Then
        $scroll_y = (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly
    EndIf
    If $scroll_x >= (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx Then
        $scroll_x = (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx
    EndIf
    ;ConsoleWrite("Min scrolly=" & - (($imgyDIFF / 2)) / $fZoom & @LF)

    ;ConsoleWrite("GUI Width = " & $GUI_Pos[2] & @LF)
    ;ConsoleWrite("Zoom= " & $fZoom & " img.width:" & $imgwidth & " img.height: " & $imgheight & " img difference =" & $imgxDIFF & @LF)
    ;ConsoleWrite("scroll_y = " & $scroll_y & @LF)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, $scroll_x, $scroll_y, $aDim[0], $aDim[1], _
            ($aDim[0] - $aDim[0] * $fZoom) / 2, ($aDim[1] - $aDim[1] * $fZoom), _
            $aDim[0] * $fZoom, $aDim[1] * $fZoom)
    _Loadimages()
EndFunc   ;==>Draw2Graphic

Func _Mousewheel($hWnd, $iMsg, $wParam, $lParam)
    Switch $wParam
        Case 0xFF880000 ;mouse wheel up
            If $fZoom > $iMinZoom Then
                $fZoom -= 0.05
                $imgwidth = $iWidth * $fZoom
                $imgheight = $iHeight * $fZoom
                $imgxDIFF = $imgwidth - $iWidth
                $imgyDIFF = $imgheight - $iHeight
                Draw2Graphic()
            EndIf
        Case 0x00780000
            $fZoom += 0.05
            $imgwidth = $iWidth * $fZoom
            $imgheight = $iHeight * $fZoom
            $imgxDIFF = $imgwidth - $iWidth
            $imgyDIFF = $imgheight - $iHeight
            Draw2Graphic()
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_Mousewheel

Func _ResetZoom()
    $fZoom = 1
    $scroll_x = 0
    $scroll_y = 0
    Global $sx, $z = 5, $sy = 0, $mc2 = 0, $f = 0, $scroll_x = 0, $scroll_y = 0, $current_iPos_y = 0, $RQGUI_POS[3] = [280, 325, 0]
    $imgwidth = $iWidth * $fZoom
    $imgheight = $iHeight * $fZoom
    $imgxDIFF = $imgwidth - $iWidth
    $imgyDIFF = $imgheight - $iHeight
    Draw2Graphic()
EndFunc   ;==>_ResetZoom
Func Terminate()
    Exit
EndFunc   ;==>Terminate

However the function im having problem with is this one:

Func _Loadimages()
    $base_x = 500
    $base_y = 300
    $start_x = -$iMinScrollx
    $start_y = -$iMinScrolly
    $ix = $start_x - $scroll_x ; pos 0 on img
    $iy = $start_y - $scroll_y ; pos 0 on img
    $xx = ($ix + $base_x) * $fZoom
    $yy = ($iy + $base_y) * $fZoom

    ConsoleWrite("$xx= " & $xx & @LF)
    ConsoleWrite("$yy= " & $yy & @LF)
    ConsoleWrite("scrolly= " & $scroll_y & @LF)
    ConsoleWrite("End_y= " & (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly & @LF)
    ConsoleWrite("$fZoom " & $fZoom & @LF)
    ; ,x:-1,y:3

    $base = 10
    $Pos_x = 3 * $base
    $hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")
    $x = _GDIPlus_ImageGetWidth($hImage_1)
    $y = _GDIPlus_ImageGetHeight($hImage_1)
    ;_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $Pos_x, 110, $x/3.5, $y/3.5)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $xx, $yy, 25, 25)
EndFunc   ;==>_Loadimages

Everytime this functions load the img it flickers like crazy (also as side note) this function will later be extended to included even more images so im really hoping to get the flickering away 

 

Any help would be greatly appreciated 

 

Edited by Acce
Posted (edited)

I found this post here:

#include <GUIConstants.au3>
#include <GDIplus.au3>

Global Const $width = 600
Global Const $height = 400
Global $title = "GDI+"

; Build your GUI here
Opt("GUIOnEventMode", 1)
$hwnd = GUICreate($title, $width, $height)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState()

; Load your GDI+ resources here:
_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $graphics)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)


While 1
    _GDIPlus_GraphicsClear($backbuffer)

    ; Draw your GDI+ scene onto $backbuffer here

    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $width, $height)
    Sleep(10)
WEnd

Func close()
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_Shutdown()
    Exit
EndFunc ;==>close

But where would I load the image ? I searched entire forum actually for some answer but so far I havent come up with the right answer ... 

Edited by Acce

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
×
×
  • Create New...