Jump to content
Sign in to follow this  
pieeater

using GDIPlus to draw on a gui

Recommended Posts

pieeater

i'm having trouble with GDIPlus, im not very good with it in the first place but i can't figure out what is wrong with my code :)

my goal is to make a gui with a randomly colored checkerboard background.

here's what i've written so far:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 500, 500, 0, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;fill gui with colors randomly

$y = 1
$finish = 0
While $finish <> 1
    Do
        If $y = 0 Then
            For $i = 0 To 500
                $c1 = Floor(Random()*0x1000000)
                $c2 = Floor(Random()*0x1000000)
                $c3 = Floor(Random()*0x1000000)
                _GDIPlus_Startup()
                $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($Form1)
                $hPen = _GDIPlus_PenCreate($hGraphic, 5, 1)
                _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, $i, 0, $hPen)
                _GDIPlus_PenSetColor($hPen, $c1 & $c2 & $c3)
                If $i >= 499 Then $y = 5
            Next
        ElseIf $y <> 0 Then
            For $i = 0 To 500
                $c1 = Floor(Random()*0x1000000)
                $c2 = Floor(Random()*0x1000000)
                $c3 = Floor(Random()*0x1000000)
                _GDIPlus_Startup()
                $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($Form1)
                $hPen = _GDIPlus_PenCreate($hGraphic, 5, 1)
                _GDIPlus_GraphicsDrawLine($hGraphic, $i, $y, $i, $i, $hPen)
                _GDIPlus_PenSetColor($hPen,"0xCF" & $c1 & $c2 & $c3)
                If $i >= 499 Then $y += 5
            Next
        Else
            MsgBox(0, "", "fail")
        EndIf
    Until $y > 190
    $finish = 1
WEnd

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

[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites
taietel

pieeater, I suck at GDI+ too, but here is a grid without:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$iW = 500
$iBoxes = 2^4
$iStep = Floor($iW/$iBoxes)
Global $aLabels[$iBoxes^2]
$hGrid = GUICreate("Grid", $iBoxes*$iStep+1, $iBoxes*$iStep+1, -1, -1, $WS_POPUP)
GUISetBkColor(0x000000)

For $i=0 To $iBoxes*$iStep-1 Step $iStep
 For $j=0 To $iBoxes*$iStep-1 Step $iStep
  If Mod(($j^2+$i^2)/$iStep^2,2) Then
   $aLabels[($j+$i*$iBoxes)/$iStep] = GUICtrlCreateLabel("", 1+$j, 1+$i, $iStep-1, $iStep-1,-1, $GUI_WS_EX_PARENTDRAG)
   GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
   GUICtrlSetBkColor(-1, Random(0x000000,0xFFFFFF,1))
  EndIf
 Next
Next
GUISetState()


While 1
Sleep(10)
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit

 EndSwitch
WEnd

Share this post


Link to post
Share on other sites
pieeater

thanks taietel :) the codes working great


[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites
pieeater

i was hoping to learn more about GDI+ but you've given me more than enough to work with for a while :) thanks for the help and i still hope someone can show me how with GDI+ because i almost never use it and it seams like it would be a really good thing to play with ;)


[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites
taietel

Here is a GDI+ version:

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

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
 Local $hGUI, $hGraphic
 Local $iW = 500
 Local $iBoxes = 2^4

 Local $iStep = Floor($iW/$iBoxes)
 
 $hGUI = GUICreate("Grid GDI+", $iBoxes*$iStep+1, $iBoxes*$iStep+1, -1, -1, $WS_POPUP)
 GUISetState()
 _GDIPlus_Startup ()
 $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
 Local $hBrush = _GDIPlus_BrushCreateSolid()
 _GDIPlus_BrushSetSolidColor($hBrush, 0xFF000000)
 _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iBoxes*$iStep+1, $iBoxes*$iStep+1, $hBrush)
 Local $hBrushGrid = _GDIPlus_BrushCreateSolid()
 For $i=0 To $iBoxes*$iStep-1 Step $iStep
  For $j=0 To $iBoxes*$iStep-1 Step $iStep
   If Mod(($j^2+$i^2)/$iStep^2,2) Then
    _GDIPlus_BrushSetSolidColor($hBrushGrid,Random(0xFF000000,0xFFFFFFFF,1))
    _GDIPlus_GraphicsFillRect($hGraphic, 1+$j, 1+$i, $iStep-1, $iStep-1, $hBrushGrid)

   EndIf
  Next
 Next

 ; Loop until user exits
 Do
 Until GUIGetMsg() = $GUI_EVENT_CLOSE

 ; Clean up resources
 _GDIPlus_BrushDispose($hBrushGrid)
 _GDIPlus_BrushDispose($hBrush)
 _GDIPlus_GraphicsDispose ($hGraphic)
 _GDIPlus_Shutdown ()

EndFunc   ;==>_Main

[EDIT] correction on those random colours

[EDIT2] forgot to delete labels variable from the previous post (without GDI+)

Edited by taietel

Share this post


Link to post
Share on other sites
monoscout999
@pieeater as taietel examples do, remember to dispose every GDI object that you create. Edited by monoscout999

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  

×