Jump to content
Sign in to follow this  
Insolence

Drawing functions

Recommended Posts

;===================================================================;
; Functions                                                   ;
;===================================================================;
Func DCConstructor(ByRef $HDC, $WindowTitle = "")
    #CS
        
        HDC GetDC(
        HWND hWnd   // handle to window
        );
        
    #CE
    Local $HWND
    
    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf
    
    $HDC = DllCall("user32.dll", "int", "GetDC", "hwnd", $HWND)
    $HDC = $HDC[0]
EndFunc;==>DCConstructor 

; Arrays passed here should be in this format:
; $array[e][0] = x
; $array[e][1] = y
Func SetPixel($HDC, $x, $y, $color)
    #CS
        
        COLORREF SetPixel(
        HDC hdc,           // handle to DC
        int X,           // x-coordinate of pixel
        int Y,           // y-coordinate of pixel
        COLORREF crColor   // pixel color
        );
        
    #CE
    Local $GDIHandle, $i
    
; If we aren't passed a variable, simply execute the call
    If IsArray($x) = 0 Then
        DllCall("Gdi32.dll", "int", "SetPixel", "int", $HDC, "int", $x, "int", $y, "int", $color)
    Else
  ; otherwise we do a group of calls
        $GDIHandle = DllOpen("Gdi32.dll")
        
        For $i = 0 To UBound($x) - 1
            DllCall($GDIHandle, "int", "SetPixel", "int", $HDC, "int", $x[$i][0], "int", $x[$i][1], "int", $color)
        Next
        
        DllClose($GDIHandle)
    EndIf
EndFunc;==>SetPixel 


Func DCDescructor(ByRef $HDC, $WindowTitle = "")
    #CS
        
        int ReleaseDC(
        HWND hWnd,  // handle to window
        HDC hDC  // handle to DC
        );
        
    #CE
    Local $HWND
    
    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf
    
    $HDC = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $HWND, "int", $HDC)
EndFunc;==>DCDescructor 

; Updated to Smed's code
Func DrawCircle($XCenter, $YCenter, $Radius, $color)
    Local $TempDC = 0
    Local $piX2 = 3.14159*2
    $deltaTheta = 1/$Radius
    Local $pixelCount = _Ceil (($piX2 + $deltaTheta)/$deltaTheta)
    
    Dim $Pixel[$pixelCount][2]
    
    For $pixelIndex = 0 To $pixelCount - 1
        $Pixel[$pixelIndex][0] = $Xcenter + Cos($pixelIndex*$deltaTheta)*$Radius
        $Pixel[$pixelIndex][1] = $Ycenter + Sin($pixelIndex*$deltaTheta)*$Radius
    Next
    
    DCConstructor($TempDC)
    
    SetPixel($TempDC, $Pixel, "", $color)
    
    DCDescructor($TempDC)
EndFunc  ;==>DrawCircle

Finished the circle one today, there may already be a GDI function for it, but I went ahead and did it anyway. Not exactly perfect, but it gets the job done.

The Circle function is dependant on all the others to work, so make sure they're in there.

(NOTE: These aren't really UDF quality functions, more like they're meant to be learned from)

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Why is it not working

GUICreate("",600,600)


Func DCConstructor(ByRef $HDC, $WindowTitle = "")
    #CS
        
        HDC GetDC(
        HWND hWnd   // handle to window
        );
        
    #CE
    Local $HWND
    
    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf
    
    $HDC = DllCall("user32.dll", "int", "GetDC", "hwnd", $HWND)
    $HDC = $HDC[0]
EndFunc;==>DCConstructor 


; Arrays passed here should be in this format:
; $array[e][0] = x
; $array[e][1] = y
Func SetPixel($HDC, $x, $y, $color)
    #CS
        
        COLORREF SetPixel(
        HDC hdc,           // handle to DC
        int X,           // x-coordinate of pixel
        int Y,           // y-coordinate of pixel
        COLORREF crColor   // pixel color
        );
        
    #CE
    Local $GDIHandle, $i
    
; If we aren't passed a variable, simply execute the call
    If IsArray($x) = 0 Then
        DllCall("Gdi32.dll", "int", "SetPixel", "int", $HDC, "int", $x, "int", $y, "int", $color)
    Else
  ; otherwise we do a group of calls
        $GDIHandle = DllOpen("Gdi32.dll")
        
        For $i = 0 To UBound($x) - 1
            DllCall($GDIHandle, "int", "SetPixel", "int", $HDC, "int", $x[$i][0], "int", $x[$i][1], "int", $color)
        Next
        
        DllClose($GDIHandle)
    EndIf
EndFunc;==>SetPixel 


Func DCDescructor(ByRef $HDC, $WindowTitle = "")
    #CS
        
        int ReleaseDC(
        HWND hWnd,  // handle to window
        HDC hDC  // handle to DC
        );
        
    #CE
    Local $HWND
    
    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf
    
    $HDC = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $HWND, "int", $HDC)
EndFunc;==>DCDescructor 

Func DrawCircle($XCenter, $YCenter, $Radius, $color)
    Local $TempDC = 0, $DCHandle, $Circumfrence, $i = 0, $Diameter, $Radius2, $y, $x
    
; Initiate array with maximum possible values
    $Circumfrence = Round(3.14 * ($Radius * 2))
    Dim $Pixel[ $Circumfrence ][2]
    
    $Radius2 = $Radius * $Radius
    $Pixel[0][0] = $XCenter
    $Pixel[0][1] = $YCenter + $Radius
    
    $Pixel[1][0] = $XCenter
    $Pixel[1][1] = $YCenter - $Radius
    
    $Pixel[2][0] = $XCenter + $Radius
    $Pixel[2][1] = $YCenter
    
    $Pixel[3][0] = $XCenter - $Radius
    $Pixel[3][1] = $YCenter
    
    $i = 4
    $y = Sqrt($Radius2 - $x * $x) + 0.5
    
    While $x < $y
        $Pixel[$i][0] = $XCenter + $x
        $Pixel[$i][1] = $YCenter + $y
        
        $Pixel[$i + 1][0] = $XCenter + $x
        $Pixel[$i + 1][1] = $YCenter - $y
        
        $Pixel[$i + 2][0] = $XCenter - $x
        $Pixel[$i + 2][1] = $YCenter + $y
        
        $Pixel[$i + 3][0] = $XCenter - $x
        $Pixel[$i + 3][1] = $YCenter - $y
        
        $Pixel[$i + 4][0] = $XCenter + $y
        $Pixel[$i + 4][1] = $YCenter + $x
        
        $Pixel[$i + 5][0] = $XCenter + $y
        $Pixel[$i + 5][1] = $YCenter - $x
        
        $Pixel[$i + 6][0] = $XCenter - $y
        $Pixel[$i + 6][1] = $YCenter + $x
        
        $Pixel[$i + 7][0] = $XCenter - $y
        $Pixel[$i + 7][1] = $YCenter - $x
        
        $i = $i + 8
        $x = $x + 1;
        $y = Sqrt($Radius2 - $x * $x) + 0.5
    WEnd
    
    If $x = $y Then
        $Pixel[$x][0] = $XCenter + $x
        $Pixel[$x][1] = $YCenter + $y
        
        $Pixel[$x + 1][0] = $XCenter + $x
        $Pixel[$x + 1][1] = $YCenter - $y
        
        $Pixel[$x + 2][0] = $XCenter - $x
        $Pixel[$x + 2][1] = $YCenter + $y
        
        $Pixel[$x + 3][0] = $XCenter - $x
        $Pixel[$x + 3][1] = $YCenter - $y
    EndIf
    
    DCConstructor($TempDC)
    
    SetPixel($TempDC, $Pixel, "", $color)
    
    DCDescructor($TempDC)
EndFunc;==>DrawCircle
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Here
DrawCircle(300, 300, 10, 0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetState()

DrawCircle(300, 300, 10, 0)

I am getting array dimension errors


.

Share this post


Link to post
Share on other sites

I had no idea, I completely stole the algorithym, and I think I messed up because it doesn't look very smooth :)


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Substitute the DrawCircle Function with this:

Func DrawCircle($XCenter, $YCenter, $Radius, $color)

  Local $TempDC = 0
  Local $piX2 = 3.14159*2
  $deltaTheta = 1/$Radius
  Local $pixelCount = _Ceil (($piX2 + $deltaTheta)/$deltaTheta)
  Dim $Pixel[$pixelCount][2]
  For $pixelIndex = 0 To $pixelCount - 1
    $Pixel[$pixelIndex][0] = $Xcenter + Cos($pixelIndex*$deltaTheta)*$Radius
    $Pixel[$pixelIndex][1] = $Ycenter + Sin($pixelIndex*$deltaTheta)*$Radius
  Next
  DCConstructor($TempDC)
  SetPixel($TempDC, $Pixel, "", $color)
  DCDescructor($TempDC)
EndFunc

cleaner, and eliminates artifacts at the n*PI/4 angles. may be slightly slower.

EDIT: oh, and works with any radius > 0

Edited by Smed

601DisengageEnd Program

Share this post


Link to post
Share on other sites

That works very well, awesome :)


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

These drawing functions will come in handy with graphical displays of information like percentage pie charts and this code will help out.

Thankx..

Share this post


Link to post
Share on other sites

I am interested and would like to see an example on how to use this. Could OP please provide one?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

A 2005 example:

;
; =============== Example ================
HotKeySet("{ESC}", "Terminate")

While Sleep(10)
    DrawCircle(300, 300, 150, 0xff0000)
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate
; ===========> End of Example =============

Func DCConstructor(ByRef $HDC, $WindowTitle = "")
    #cs
        
        HDC GetDC(
        HWND hWnd   // handle to window
        );
        
    #CE
    Local $HWND

    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf

    $HDC = DllCall("user32.dll", "int", "GetDC", "hwnd", $HWND)
    $HDC = $HDC[0]
EndFunc   ;==>DCConstructor


; Arrays passed here should be in this format:
; $array[e][0] = x
; $array[e][1] = y
Func SetPixel($HDC, $x, $y, $color)
    #CS
        
        COLORREF SetPixel(
        HDC hdc,           // handle to DC
        int X,           // x-coordinate of pixel
        int Y,           // y-coordinate of pixel
        COLORREF crColor   // pixel color
        );
        
    #CE
    Local $GDIHandle, $i

    ; If we aren't passed a variable, simply execute the call
    If IsArray($x) = 0 Then
        DllCall("Gdi32.dll", "int", "SetPixel", "int", $HDC, "int", $x, "int", $y, "int", $color)
    Else
        ; otherwise we do a group of calls
        $GDIHandle = DllOpen("Gdi32.dll")

        For $i = 0 To UBound($x) - 1
            DllCall($GDIHandle, "int", "SetPixel", "int", $HDC, "int", $x[$i][0], "int", $x[$i][1], "int", $color)
        Next

        DllClose($GDIHandle)
    EndIf
EndFunc   ;==>SetPixel


Func DCDescructor(ByRef $HDC, $WindowTitle = "")
    #CS
        
        int ReleaseDC(
        HWND hWnd,  // handle to window
        HDC hDC  // handle to DC
        );
        
    #CE
    Local $HWND

    If $WindowTitle = "" Then
        $HWND = 0
    Else
        $HWND = WinGetHandle($WindowTitle)
    EndIf

    $HDC = DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $HWND, "int", $HDC)
EndFunc   ;==>DCDescructor

Func DrawCircle($XCenter, $YCenter, $Radius, $color)

    Local $TempDC = 0
    Local $piX2 = 3.14159 * 2
    $deltaTheta = 1 / $Radius
    Local $pixelCount = Ceiling(($piX2 + $deltaTheta) / $deltaTheta)
    Dim $Pixel[$pixelCount][2]
    For $pixelIndex = 0 To $pixelCount - 1
        $Pixel[$pixelIndex][0] = $XCenter + Cos($pixelIndex * $deltaTheta) * $Radius
        $Pixel[$pixelIndex][1] = $YCenter + Sin($pixelIndex * $deltaTheta) * $Radius
    Next
    DCConstructor($TempDC)
    SetPixel($TempDC, $Pixel, "", $color)
    DCDescructor($TempDC)
EndFunc   ;==>DrawCircle
;

Share this post


Link to post
Share on other sites

i got it working.. thank you.. i just have a question..

i saw a while back there was a way to set a state to be always on top.. but i cant find that thread anymore.. does anyone know how i would do this??

Share this post


Link to post
Share on other sites

i got it working.. thank you.. i just have a question..

i saw a while back there was a way to set a state to be always on top.. but i cant find that thread anymore.. does anyone know how i would do this??

You will have to redraw the picture over and over again. The easiest way would be to create a window and doublebuffer the window. Search the forum for monoceres' doublebuffering template. It really helped me out.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...