Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How could I draw a filled triangle??

Status
Not open for further replies.

nexius

Programmer
Jul 8, 2000
109
CA
How could I draw a filled triangle??

Is this possible?
 
Ok I found an old thread that answered my question. But for some odd reason it won't work when I try to use line coordinates for the points of the polygon!

If I replace the line1.x1's with numbers it works fine.... what's the problem??

Here's what won't work:
Code:
Option Explicit
Private Declare Function SetPolyFillMode Lib "gdi32" (ByVal hdc As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Const WINDING = 2

Private Sub Command1_Click()
    Dim pts() As POINTAPI
    
    ReDim pts(2)
    
    pts(0).x = Line1.X1
    pts(0).y = Line1.Y1
    pts(1).x = Line2.X1
    pts(1).y = Line2.Y1
    pts(2).x = Line3.X1
    pts(2).y = Line3.Y1
    
    Form1.FillStyle = 0 ' solid fill
    
    Call SetPolyFillMode(Form1.hdc, WINDING)
    Call Polygon(Form1.hdc, pts(0), 3)
    
End Sub
 
Try this example from API Guide

Private Type COORD
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: 'E-Mail: KPDTeam@Allapi.net
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
 
NeXius, your problem is almost certainly that the Scalemode of your form is Twips, rather than pixels, which will effect the values returned as Line coordinates. The Polygon function you are using works in pixels.

I'd ignore the AllAPI solution. It seems to me that they quite often go overboard - and their triangle-filling routine above is a good example of that
 

This thread711-420927 contains the FloodFill API that may also work for you.

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top