0

下面的代码是 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)
4

2 回答 2

1

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

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

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

于 2012-08-11T23:07:51.873 回答
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
于 2012-08-12T00:06:04.883 回答