Jump to content

Drawing functions


Insolence
 Share

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.
Link to comment
Share on other sites

  • 1 month later...

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

.

Link to comment
Share on other sites

the smalles radius it will accept is 15.

GUISetState()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Here
DrawCircle(300, 300, 15, 0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
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.
Link to comment
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

Link to comment
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.
Link to comment
Share on other sites

  • 4 years later...

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
;

Link to comment
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.

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