Jump to content
Sign in to follow this  
ghetek

Making a resizeable graph in GDI

Recommended Posts

ghetek

Hey guys,

I am trying to create a resizeable graph on a GUI. This works but there must be a better way, right? Any help would be greatly appreciated.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Opt('MustDeclareVars', 1)
Global $hGUI, $sGUI_Title = 'Test Resizer', $hGraphic, $hPen, _
        $sGUI_Width = 600, $sGUI_Height = 600, _
        $hGUI = GUICreate($sGUI_Title, $sGUI_Width, $sGUI_Height, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)

_GDIPlus_Startup()
GUISetState()
_DrawGraph()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_RESIZED
            $sGUI_Width = _WinAPI_GetWindowWidth($hGUI)
            $sGUI_Height = _WinAPI_GetWindowHeight($hGUI)
            ConsoleWrite('RESIZE Width:' & $sGUI_Width & ' Height:' & $sGUI_Height & @CRLF)
            _DrawGraph($sGUI_Width, $sGUI_Height)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

_GDIPlus_Shutdown()

Func _DrawGraph($width = $sGUI_Width, $height = $sGUI_Height)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hPen = _GDIPlus_PenCreate()
    ;Draw graph
    $width = $width - 50 ; just some padding
    $height = $height - 90 ; just some padding
    _GDIPlus_GraphicsDrawLine($hGraphic, 10, Int($height/2), $width + 10, Int($height/2), $hPen)
    _GDIPlus_GraphicsDrawLine($hGraphic, Int($width/2), 10, Int($width/2), $height + 10, $hPen)

    _GDIPlus_GraphicsDrawRect($hGraphic, 10, 10, $width, $height, $hPen)

    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)

EndFunc   ;==>_DrawGraph
Edited by ghetek

Share this post


Link to post
Share on other sites
Yoriz

Hey, this might be a bit better.

I added a repaint of the window.

made the pen only created and destroyed once

changed the $GUI_EVENT_RESIZED into a GUIRegisterMsg on resize

made the resizing scale better and added a variable $fMaintainAspect that can be set true or false depending on how you prefere it.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

Global $hGUI, $sGUI_Title = 'Test Resizer', $hGraphic, $hGraphicGUI, $hBMPBuff, $hPen, _
        $sGUI_Width = 574, $sGUI_Height = 600, _
        $hGUI = GUICreate($sGUI_Title, $sGUI_Width, $sGUI_Height, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)

GUISetState()

_GDIPlus_Startup()
$hPen = _GDIPlus_PenCreate()
_DrawGraph()

GUIRegisterMsg($WM_SIZE, "_DrawGraph")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _CleanUpGdi()
            Exit
    EndSwitch
WEnd

Func _CleanUpGdi()
    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>_CleanUpGdi

Func _DrawGraph()
    Local $fMaintainAspect = True
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)

    Local $iWinSIze = WinGetClientSize($hGUI)
    Local $iBorder = 10
    Local $iWidth = Round($iWinSIze[0] - $iBorder * 2)
    Local $iHeight = Round($iWinSIze[1] - $iBorder * 2)
    If $fMaintainAspect Then
        If $iHeight < $iWidth Then $iWidth = $iHeight
        If $iWidth < $iHeight Then $iHeight = $iWidth
    EndIf
    ConsoleWrite('Width:' & $iWinSIze[0] & ' Height:' & $iWinSIze[1] & @CRLF)

    $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($iWidth + $iBorder * 2, $iHeight + $iBorder * 2, $hGraphicGUI)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)

    ;Draw graph
    _GDIPlus_GraphicsDrawLine($hGraphic, $iBorder, $iBorder + $iHeight / 2, $iBorder + $iWidth, $iBorder + $iHeight / 2, $hPen)
    _GDIPlus_GraphicsDrawLine($hGraphic, $iBorder + $iWidth / 2, $iBorder, $iBorder + $iWidth / 2, $iBorder + $iHeight, $hPen)
    _GDIPlus_GraphicsDrawRect($hGraphic, $iBorder, $iBorder, $iWidth, $iHeight, $hPen)

    GUIRegisterMsg($WM_PAINT, "MY_PAINT")
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hGUI, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME))

    Return $GUI_RUNDEFMSG
EndFunc   ;==>_DrawGraph

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_PAINT

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.