我正在测试如何在单击按钮时绘制一条将按钮连接到另一个按钮的线,我对坐标和坐标感到困惑,settop
setleft
它们实际上是如何简单地工作的。我知道我们必须设置X2
Y2
线的(起点与终点相减),但我真的很困惑要减去什么以及如何做到这一点。
到目前为止,这是我尝试过的:
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;
}
我正在尝试从 btn 到 btn2 画一条线。
而且,我如何将按钮调整到同一级别,现在右按钮(btn2)比左按钮(btn)低一点,我想画一条线将右按钮连接到左按钮点击按钮 0 ,所以 0 将画一条线到 0 。