简体   繁体   English

WPF C#画布绘图线连接按钮

[英]WPF C# Canvas Drawing Line connecting buttons

i am testing out how to draw a line that connects the button to another button upon clicking the the button, i got confuse with the coordinates and the settop setleft , how do they actually work in simple terms. 我测试了如何绘制在点击按钮按钮连接到另一个按钮一条线,我得到的坐标和迷惑settop setleft ,他们是如何在实际深入浅出工作。 i know that we have to set the X2 Y2 ( subtract of start point with end point ) of the line, but i really confuse at what to subtract and how do i do this . 我知道我们必须设置直线的X2 Y2 (起点与终点的差值),但是我真的对减法以及如何做到这一点感到困惑。

this is so far what i have tried: 到目前为止,这是我尝试过的:

 int k = 20;
 for (int i = 0; i < 4; i++)
 {
      Button btn = new Button();
      btn.Content = i.ToString();
      btn.Height = 20;
      btn.Width = 20;
      Canvas.SetTop(btn,k); // 20
      Canvas.SetLeft(btn, 20); // 10 
      Canvas1.Children.Add(btn);
      btn.PreviewMouseDown += (source, e) =>
      {
          // No idea how to set X2 , Y2 for the line's end point.
          Line line = new Line();
          //line.X2 = ;
          //line.Y2 = ;
          line.Stroke = Brushes.Red;
          line.StrokeThickness = 4;
          Canvas.SetLeft(line,40); // Suppose this is where the line should start 
          Canvas.SetTop(line ,40); // for button " 0 " .
          Canvas1.Children.Add(line);
      };
      k += 20;
 }
 for (int i = 0; i < 4; i++)
 {
      Button btn2 = new Button();
      btn2.Content = i.ToString();
      btn2.Height = 20;
      btn2.Width = 20;
      Canvas.SetTop(btn2, k); // 20
      Canvas.SetRight(btn2, 20); // 10
      Canvas1.Children.Add(btn2);
      btn2.PreviewMouseDown += (source, e) =>
      {
          //Draw Line to connect here.
      };
      k += 20;
  }

i am trying to draw a line from btn to btn2 . 我正在尝试从btn到btn2划清界限。

预览图片

And also , how do i adjust the buttons to be in the same level, for now the right buttons ( btn2 ) is abit lower than the left buttons ( btn) and i want to draw a line to connect the right buttons to the left buttons upon clicking button 0 , so 0 will draw a line to 0 . 而且,我如何将按钮调整为同一水平,因为现在右按钮(btn2)比左按钮(btn)略低,我想画一条线将右按钮连接到左按钮在单击按钮0时,因此0将在0处画一条线。

Whilst you can do it by moving the Canvas around, you might be better off using the positions of the Buttons themselves on the Canvas . 尽管可以通过移动Canvas来做到这一点,但最好还是使用Buttons本身在Canvas上的位置。

Essentially, you need to have two sets of coordinates, your LineStart and LineEnd , which in this case will be obtained from the two different Buttons you're clicking. 本质上,您需要具有两组坐标,即LineStartLineEnd ,在这种情况下,这是从您单击的两个不同Buttons获得的。

You could use Point location = ((Button)source).TransformToAncestor(Canvas1).Transform(new Point(0, 0)) to obtain the location of the button being clicked, relative to the parent Canvas . 您可以使用Point location = ((Button)source).TransformToAncestor(Canvas1).Transform(new Point(0, 0))获得相对于父级Canvas而言被单击的按钮的位置。

You would then need to do a similar thing to find the location of the original object that you had clicked (which you don't seem to be storing). 然后,您需要做类似的事情来找到您单击的原始对象的位置(您似乎没有在存储它)。 You could then simply draw a line between the two calculated Points by setting the X1, X2, Y1, Y2 values. 然后,您可以通过设置X1, X2, Y1, Y2值在两个计算的Points之间简单地画一条线。

Adapting your example, you could do something like this (just to demonstrate): 改编您的示例,您可以执行以下操作(仅用于演示):

public partial class MainWindow : Window
{
    public Button LastClicked { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        InitButtons();
    }


    public void InitButtons()
    {
        int k = 20;

        for (int i = 0; i < 4; i++)
        {
            Button btn = new Button();
            btn.Content = i.ToString();
            btn.Height = 20;
            btn.Width = 20;
            Canvas.SetTop(btn, k); // 20
            Canvas.SetLeft(btn, 20); // 10 
            Canvas1.Children.Add(btn);
            btn.PreviewMouseDown += (source, e) =>
            {
                if (LastClicked != null)
                {
                    // Get button locations.
                    Point LastClickedLocation = LastClicked.TransformToAncestor(Canvas1).Transform(new Point(0, 0));
                    Point ThisClickedLocation = ((Button)source).TransformToAncestor(Canvas1).Transform(new Point(0, 0));

                    // Stop same side lines.
                    if (LastClickedLocation.X != ThisClickedLocation.X)
                    {
                        Line line = new Line();

                        line.X1 = LastClickedLocation.X;
                        line.Y1 = LastClickedLocation.Y + btn.Height / 2; // Button Middle.
                        line.X2 = ThisClickedLocation.X + btn.Width; // Adjust Left side.
                        line.Y2 = ThisClickedLocation.Y + btn.Height / 2; // Button Middle.

                        line.Stroke = Brushes.Red;
                        line.StrokeThickness = 4;

                        Canvas1.Children.Add(line);
                    }
                }

                LastClicked = (Button)source;
            };
            k += 20;
        }

        k = 20; // Reset k, this is why your buttons weren't aligned.

        for (int i = 0; i < 4; i++)
        {
            Button btn2 = new Button();
            btn2.Content = i.ToString();
            btn2.Height = 20;
            btn2.Width = 20;
            Canvas.SetTop(btn2, k); // 20
            Canvas.SetRight(btn2, 20); // 10
            Canvas1.Children.Add(btn2);

            btn2.PreviewMouseDown += (source, e) =>
            {
                if (LastClicked != null)
                {
                    // Get button locations.
                    Point LastClickedLocation = LastClicked.TransformToAncestor(Canvas1).Transform(new Point(0, 0));
                    Point ThisClickedLocation = ((Button)source).TransformToAncestor(Canvas1).Transform(new Point(0, 0));

                    // Stop same side lines.
                    if (LastClickedLocation.X != ThisClickedLocation.X)
                    {
                        Line line = new Line();

                        line.X1 = LastClickedLocation.X + btn2.Width; // Adjust Left side.
                        line.Y1 = LastClickedLocation.Y + btn2.Height / 2; // Button Middle.
                        line.X2 = ThisClickedLocation.X;
                        line.Y2 = ThisClickedLocation.Y + btn2.Height / 2; // Button Middle.

                        line.Stroke = Brushes.Red;
                        line.StrokeThickness = 4;

                        Canvas1.Children.Add(line);
                    }
                }

                LastClicked = (Button)source;
            };
            k += 20;
        }
    }
}

I wouldn't recommend using this exact code, as it's not particularly bright about handling Button clicks and deciding when to draw lines, but it should demonstrate the drawing and obtaining coordinates that you're after. 我不建议使用此确切的代码,因为它在处理Button单击和确定何时绘制线条方面并不是特别出色,但是它应该演示绘制并获取您要使用的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM