在WPF中绘制折线

时间:2012-08-11 20:50:43

标签: .net wpf vb.net

以下代码是Microsoft生成折线的示例。我已经实现了这个,但它不起作用。

我的问题是:使“myPolyline”对象显示的方法是什么?代码中的最后一行是myGrid.Children.Add(myPolyline),我不明白哪个对象应该是名为“myGrid”,也许我的问题的答案与此有关。

' Add a Polyline Element
Dim myPolyline As New Polyline()
myPolyline.Stroke = Brushes.SlateGray
myPolyline.StrokeThickness = 2
myPolyline.FillRule = FillRule.EvenOdd
Dim Point4 As New System.Windows.Point(1, 50)
Dim Point5 As New System.Windows.Point(10, 80)
Dim Point6 As New System.Windows.Point(20, 40)
Dim myPointCollection2 As New PointCollection()
myPointCollection2.Add(Point4)
myPointCollection2.Add(Point5)
myPointCollection2.Add(Point6)
myPolyline.Points = myPointCollection2
myGrid.Children.Add(myPolyline)

2 个答案:

答案 0 :(得分:1)

myGrid只是添加到应用了名称为myGrid的WPF表单的默认Grid

<Grid x:Name="myGrid">
</Grid>

您从示例页面How to: Draw a Polyline by Using the Polyline Element开始显示代码的示例,其中包含指向可下载示例项目的链接,其中还包含其他形状元素的示例。

答案 1 :(得分:0)

Winforms中的类似函数是Graphics.DrawLines方法或Graphics.DrawPath方法,您可以在希望绘制的控件的Paint事件中使用它。

快速示例:

Private Sub Panel1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    Dim myPen As Pen = New Pen(Brushes.DarkGray, 2)
    Dim myPoints() As Point = New Point() {New Point(1, 50), New Point(10, 80), New Point(20, 40)}
    e.Graphics.DrawLines(myPen, myPoints)
End Sub

和DrawPath示例:

Private Sub Panel1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    Dim myPen As Pen = New Pen(Brushes.DarkGray, 2)
    Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath

    path.AddLine(New Point(1, 50), New Point(10, 80))
    path.AddLine(New Point(10, 80), New Point(20, 40))
    path.CloseFigure()

    e.Graphics.DrawPath(myPen, path)
End Sub