当前位置:编程学习 > VB >>

如何用VB实现由很多个已知点坐标生成多边形?

本来是要生成三维图形的,只是为了方便先把Z值设为0,如何实现啊?就是许多个多边形相连,就想道路一样的  比如50个点 --------------------编程问答--------------------

Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long


【操作系统】
Win9X:Yes
WinNT:Yes
【说明】
  描绘一个多边形,由两点或三点的任意系列构成。windows会将最后一个点与第一个点连接起来,从而封闭多边形。多边形的边框用当前选定的画笔描绘,多边形用当前选定的刷子填充 
【返回值】
  Long,非零表示成功,零表示失败。会设置GetLastError 
【其它】
  GetPolyFillMode
  和 SetPolyFillMode
  函数决定了如何在多边形内部填充
【参数表】
  hdc ------------  Long,用于描绘的设备场景
  lpPoint --------  POINTAPI,在nCount POINTAPI结构数组中的第一个POINTAPI结构
  nCount ---------  Long,多边形的总点数(顶点数)
--------------------编程问答--------------------

'Example Name:Polygons
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: http://www.allapi.net/
    '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


--------------------编程问答-------------------- 方法1:
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
会有填充

方法2:
Private Declare Function Polyline Lib "gdi32" Alias "Polyline" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
没有填充,但是,最后一点必须为开始点坐标,即给定的点必须要比要画的点数多1,多出来的为最后点,其坐标为第一点坐标(否则曲线不会封闭)

补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,