|
-
Nov 7th, 2001, 10:18 AM
#1
Thread Starter
Junior Member
Drawing and Filling a Polygon
Excuse this apology for a programmer. I want to draw/fill a polygon. So have declared:
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Type POINTAPI
x As Long
y As Long
End Type
I assume that nCount asks for the number of vertices, nPolyFillMode is the type of fill and lppoint asks for the vertices.
I've started my code as:
Dim A As POINTAPI
Dim B As POINTAPI
Dim C As POINTAPI
Private Sub Form_Load()
Picture1.Scale (-200, -200)-(1200, 1200)
With Picture1
A.x = 0
A.y = 0
B.x = 0
B.y = 1000
C.x = 1000
C.y = 0
And now I want to call CreatePolygonRgn . I know that nCount = 3. I'll set nPolyFillMode as alternate.
But how do I input lpPoint? Using A - B - C or A,B,C is clearly no good. So do I have to set the points up as an array?
-
Nov 7th, 2001, 10:28 AM
#2
This might help filling a polygon on a form -
from www.allapi.net
Code:
Private Type COORD ' like POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Private Sub Form_Paint()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
Me.Cls
' Number of vertices in polygon.
NumCoords = 3
' Set scalemode to pixels to set up points of triangle.
Me.ScaleMode = vbPixels
' Assign values to points.
poly(1).x = Form1.ScaleWidth / 2
poly(1).y = Form1.ScaleHeight / 2
poly(2).x = Form1.ScaleWidth / 4
poly(2).y = 3 * Form1.ScaleHeight / 4
poly(3).x = 3 * Form1.ScaleWidth / 4
poly(3).y = 3 * Form1.ScaleHeight / 4
' Polygon function creates unfilled polygon on screen.
' Remark FillRgn statement to see results.
Polygon Me.hdc, poly(1), NumCoords
' Gets stock black brush.
hBrush = GetStockObject(BLACKBRUSH)
' Creates region to fill with color.
hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
' If the creation of the region was successful then color.
If hRgn Then FillRgn Me.hdc, hRgn, hBrush
DeleteObject hRgn
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
-
Nov 7th, 2001, 10:44 AM
#3
Thread Starter
Junior Member
Thanks,
I've already tried this.
It crashes at:
Dim poly(1 To 3) As COORD
with "user defined type not defined".
I didn't find this message v helpful. Are any error messages ever helpful?
-
Nov 7th, 2001, 10:50 AM
#4
What it's saying is that COORD is not the same set of characters
in the Private type COORD
and in the Dim Poly(1 to 3) as COORD statements
I don't have VB here, but I also don't see why.
Separate the very long Dim statement line --- Dim x.. y...
out into muiltple one line statements
Dim x...
Dim Y...
Dim z...
This may help you find if the error is actually in another part of the Dim statement.
-
Nov 7th, 2001, 01:40 PM
#5
Thread Starter
Junior Member
Can anyone tell me why this code (with all relevant functions and types declared) doesn't deliver:
Dim Vertex(1 To 3) As POINTAPI
Private Sub Form_Load()
Height = Screen.Height
Width = 12030
Picture1.Scale (-200, -200)-(1200, 1200)
With Picture1
Vertex(1).x = Picture1.ScaleWidth - 1200
Vertex(1).y = Picture1.ScaleHeight + 1200
Vertex(2).x = Picture1.ScaleWidth - 1000
Vertex(2).y = Picture1.ScaleHeight - 500
Vertex(3).x = Picture1.ScaleWidth - 600
Vertex(3).y = Picture1.ScaleHeight - 600
hBrush% = CreateSolidBrush(&HC0E0FF)
bool% = Polygon(Picture1.hDC, Vertex(1), 3)
hRgn% = CreatePolygonRgn(Vertex(1), 3, ALTERNATE)
If hRgn% Then
bool% = FillRgn(Picture1.hDC, hRgn%, hBrush%)
End If
End With
End Sub
Polygon draws nothing.
Also hBrush% = CreateSolidBrush(&HC0E0FF) generates an overflow.
-
Nov 7th, 2001, 01:48 PM
#6
For starters you want longs for all those handles.
I never use the % & business. But substitute & for % to get longs.
Also api's return values. Check the value returned for success.
You can get more information on errors
Code:
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As _
Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Dim Buffer As String
Buffer = Space(200)
'Format the message string
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, _
GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
MsgBox Buffer
End Sub
-
Nov 8th, 2001, 04:26 AM
#7
Thread Starter
Junior Member
Thanks for response. The code below (with all function/type declarations) now runs without error but still doesn't deliver. Would be grateful for any suggestions.
Dim Vertex(1 To 3) As POINTAPI
Dim hbrush As Long
Dim polyA As Long
Dim hRgn As Long
Dim filled As Long
Private Sub Form_Load()
Height = Screen.Height
Width = 12030
Picture1.Scale (-200, -200)-(1200, 1200)
With Picture1
Vertex(1).x = Picture1.ScaleWidth - 1200
Vertex(1).y = Picture1.ScaleHeight - 1200
Vertex(2).x = Picture1.ScaleWidth - 1000
Vertex(2).y = Picture1.ScaleHeight - 500
Vertex(3).x = Picture1.ScaleWidth - 600
Vertex(3).y = Picture1.ScaleHeight - 600
hbrush = CreateSolidBrush(&HC0E0FF)
polyA = Polygon(Picture1, Vertex(1), 3)
hRgn = CreatePolygonRgn(Vertex(1), 3, ALTERNATE)
If hRgn Then
filled = FillRgn(Picture1, hRgn, hbrush)
End If
End With
End Sub
-
Nov 8th, 2001, 08:25 AM
#8
PowerPoster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|