Recommended Posts

I am still trying to make the needle of my clocks a little nicer I want to use Polygon windows api function.

Here is the quote from MSDN :

BOOL Polygon(

HDC hdc, // handle to DC

CONST POINT *lpPoints, // polygon vertices

int nCount // count of polygon vertices

);

Would it be possible to use that function?

If so how could I call it with an array of 3 points?

Right now I settled with just drawing 3 lines to make it , but it seems to be somewhat cpu heavy and I get some flickering (This time because it just takes a while to draw them).

Share on other sites

Well I am using the default brush (not selecting any).

First of all I need to calculate a point of intersection of a line and a circle to determine the length of those lines.

Then I draw a line from a place on a circle towards its center ,small horizontal line and then line back to the circle where it started.

I am using regular solid pen with width of 1.

Of course I draw everything twice because first I need to erase the previous graphic (just drawing with background color) then drawing with black.

Here is some code to illustrate

CODE

Func Update() ;Updates position of a needle (Just for testing)

\$point = GetPoint(\$x)

;\$point1 = GetPoint(\$x + 5)

DrawLine(\$point , \$point , 59, 70 , 1, 0xC8D0D4)

DrawLine(59 , 70 , 61 , 70, 2, 0xC8D0D4)

DrawLine(61 , 70 , \$point , \$point, 1, 0xC8D0D4)

;UpdateLinePosition(\$x + 5 , \$y + 5, 70 , 70, \$x , \$y , 70, 70)

\$x = \$x + 5

;\$y = 10

if \$x > 110 Then

\$x = 10

;\$y = 10

EndIf

\$point = GetPoint(\$x)

DrawLine(\$point , \$point , 59, 70 , 1, 0x0)

DrawLine(59 , 70 , 61 , 70, 2, 0x0)

DrawLine(61 , 70 , \$point , \$point, 1, 0x0)

EndFunc

Func getPoint(\$coordX );y is assumed to be 10 (draw a line )

Dim \$result

Local \$coordY = 10

Local \$a1, \$b1 ,\$a, \$b , \$c

if \$coordX == 60 Then

\$result = 60

\$result = 20

Return \$result

EndIf

\$a1 = (70 - \$coordY ) / (60 - \$coordX );determine tangent of a line passing through given point and circle center

\$b1 = 70 - (\$a1 * 60 )

\$a = (\$a1 * \$a1) + 1

\$b = -120 - (140 * \$a1) + (2 * \$a1 * \$b1 )

\$c = (\$b1 * \$b1 ) - (140 * \$b1 ) + 6000

\$b24ac = sqrt((\$b * \$ - (4 * \$a * \$c))

\$result1X = ( -\$b + \$b24ac) / (2* \$a)

\$result2X = ( -\$b - \$b24ac) / (2* \$a)

\$result1Y = \$a1 * \$result1X + \$b1

\$result2Y = \$a1 * \$result2X + \$b1

if \$result1Y < 70 Then

\$result = Round(\$result1X)

\$result = Round(\$result1Y)

Else

\$result = Round(\$result2X)

\$result = Round(\$result2Y)

EndIf

;ConsoleWrite(\$result1X & " " & \$result1Y & @CRLF & \$result2X & " " & \$result2Y & @CRLF & @CRLF)

return \$result

EndFunc

Func DrawLine(\$fromX , \$fromY , \$xTo , \$yTo, \$width , \$color )

\$pic_hWnd = ControlGetHandle(\$gui,"" ,\$graphic)

\$user_dll = DllOpen("user32.dll")

\$gdi_dll = DllOpen("gdi32.dll")

\$l_hdc = DLLCall(\$user_dll,"int","GetDC","hwnd",\$pic_hWnd)

\$pen = DLLCall(\$gdi_dll,"int","CreatePen","int",0x0,"int",\$width,"int",\$color)

\$obj_orig = DLLCall(\$gdi_dll,"int","SelectObject","int",\$l_hdc,"int",\$pen)

DLLCall(\$gdi_dll,"int","MoveToEx","int",\$l_hdc,"int",\$fromX,"int",\$fromY,"int",0)

DLLCall(\$gdi_dll,"int","LineTo","int",\$l_hdc,"int",\$xTo,"int",\$yTo) ;draw line

DLLCall(\$gdi_dll,"int","SelectObject","int",\$l_hdc,"int",\$obj_orig)

DLLCall(\$gdi_dll,"int","DeleteObject","int",\$pen)

\$l_hdc = DLLCall(\$user_dll,"int","ReleaseDC","hwnd",\$pic_hWnd,"int",\$l_hdc)

DllClose(\$gdi_dll)

DllClose(\$user_dll)

EndFunc

Edited by matrix200

Share on other sites

Look at my post here - this concept not tested but I think it should work:

```\$apoints = DllStructCreate("int,int,int,int,int,int") ; 3x point: X,Y
DllStructSetData(\$apoints, 1, 10) ; X1
DllStructSetData(\$apoints, 2, 11) ; Y1
DllStructSetData(\$apoints, 3, 20) ; X2
DllStructSetData(\$apoints, 4, 21) ; Y2
DllStructSetData(\$apoints, 5, 30) ; X3
DllStructSetData(\$apoints, 6, 31) ; Y3
_Polygon(\$hdc, DllStructGetPtr(\$apoints), 3)```

_Polygon() is wrapper for DllCall()

Share on other sites

Well the function fails for some reason.

I am getting 0 as a result which means a failure according to MSDN.

The question is what kind of pointer type I should specify.

I did just "ptr" , maybe I should use some special kind of pointer?

Like long_ptr or int_ptr?