如何在边界上画一条将其分成两半的直线

时间:2019-06-26 09:07:20

标签: c# wpf draw

我在wpf应用程序中有一个边框。

  1. 如何在边界上画一条将其分成两半的直线?
  2. 我有一个单击边框的处理程序。在MouseDown之后如何在按下(p.Y,p.X)的点处绘制黑点?在第二次按下后如何擦除旧的并绘制新的?

1 个答案:

答案 0 :(得分:0)

要绘制一条线来划分边界,可以使用“线”或“折线”,如下所示:

(您需要找到边框的高度和重量来获得半点,假设是200,300)

public MainWindow()
        {
            InitializeComponent();
            canvas.Children.Clear();
            Point[] points = new Point[2]
            {
            new Point(0,  100),
            new Point(300 , 100)
            };
            DrawLine(points);

        }

private void DrawLine(Point[] points)
        {
            Polyline line = new Polyline();
            PointCollection collection = new PointCollection();
            foreach (Point p in points)
            {
                collection.Add(p);
            }
            line.Points = collection;
            line.Stroke = new SolidColorBrush(Colors.Red);
            line.StrokeThickness = 1;
            canvas.Children.Add(line);
        }

要绘制点,可以使用椭圆,也可以使用e.GetPosition()来获得鼠标单击的位置:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    \\ Remove the last object from the canvas
     if(Canvas.Children.Count>0)
    canvas.Children.RemoveAt(canvas.Children.Count-1);

    Ellipse ellipse = new Ellipse();
    ellipse.Fill = Brushes.Sienna;
    ellipse.Width = 10;
    ellipse.Height = 10;
    ellipse.StrokeThickness = 2;

    canvas.Children.Add(ellipse);

    \\Set the position of the point.
    Canvas.SetLeft(ellipse, e.GetPosition(canvas).X);
    Canvas.SetTop(ellipse, e.GetPosition(canvas).Y);
}