0

我有两个UIElements(即矩形)Canvas和它们的坐标。如何将它们与后面的代码中的 arc 连接起来?

4

1 回答 1

2

无需对矩形(或其他对象)进行精确命中:确保 Z 顺序正确。arc.SetValue(Canvas.ZIndex, -1)会将其推到后台。如果你想要一个垂直的命中,你需要打破代数:/

对于弧:(请参阅http://msdn.microsoft.com/en-us/library/ms751808.aspx),它需要包含在 PathFigure 中。

编辑:这显示了两个连接的矩形。简单的线在两个中心之间运行。圆弧从一个中心(pathFigure 起点)开始,第一个参数是第二个对象的中心。

        r1 = new Rectangle();
        r1.Margin = new Thickness(50, 50, 0, 0);
        r1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        r1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        r1.Height = 50;
        r1.Width= 50;
        r1.Fill = new SolidColorBrush(Colors.Red);


        r2 = new Rectangle();
        r2.Width = 50;
        r2.Height = 50;
        r2.Fill = new SolidColorBrush(Colors.Blue);
        r2.Margin = new Thickness(350, 450, 0, 0);
        r2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        r2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

        l = new Line();
        l.X1 = 75;
        l.Y1 = 75;
        l.X2 = 375;
        l.Y2 = 475;
        l.Fill = new SolidColorBrush(Colors.Purple);
        l.Stroke = new SolidColorBrush(Colors.Purple);
        l.StrokeThickness = 2;
        l.SetValue(Canvas.ZIndexProperty, -1);

        PathGeometry myPathGeometry = new PathGeometry();

        // Create a figure.
        PathFigure pathFigure1 = new PathFigure();
        pathFigure1.StartPoint = new Point(75, 75);

        pathFigure1.Segments.Add(
            new ArcSegment(
                new Point(375, 475),
                new Size(50, 50),
                45,
                true, /* IsLargeArc */
                SweepDirection.Clockwise,
                true /* IsStroked */ ));
        myPathGeometry.Figures.Add(pathFigure1);

        // Display the PathGeometry. 
        Path myPath = new Path();
        myPath.Stroke = Brushes.Black;

        myPath.StrokeThickness = 1;
        myPath.Data = myPathGeometry;
        myPath.SetValue(Canvas.ZIndexProperty, -1);

        LayoutRoot.Children.Add(r1);
        LayoutRoot.Children.Add(r2);
        LayoutRoot.Children.Add(l);
        LayoutRoot.Children.Add(myPath);
于 2012-05-09T15:19:01.760 回答