如何在vb.net中的两个按钮之间画线?

时间:2016-04-12 16:20:33

标签: vb.net

我有9个按钮,在vb.net中按3 * 3矩阵排列。

btn1    btn2     btn3
btn4    btn5     btn6
btn7    btn8     btn9

假设,如果我点击btn1然后将我的鼠标移动到btn2然后移动btn3,最后点击btn6然后行应该从btn1到btn2到btn3到btn6。

像这样:

btn1----btn2----btn3
                 |
btn4    btn5    btn6

btn7    btn8    btn9

假设,如果我首先点击btn6然后将我的鼠标移动到btn9到btn8到btn5然后最后点击btn2那么就应该用这样的方式绘制线条:

btn1    btn2    btn3

btn4    btn5    btn6
         |        |
btn7    btn8----btn9

如何绘制这种线条? 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要在Paint事件中执行此操作;在表单上或添加一个图片框并将事件添加到它。 目标是提供操作员拖动鼠标的数字“键盘”吗?如果是这种情况,那么我会添加图片框并在图片框的绘图事件中绘制“按钮”。这样您就可以存储“按钮”的位置,并且在代码中可以捕获mouseDown和mouseMove事件,以确定单击鼠标和移动鼠标的按钮。绘制线条使用e.graphics.DrawLine(pens.black,x1,y1,x2,y2)函数。 如果你在表单上执行它,那么你需要知道按钮的位置和它们之间的绘制,这些可以在程序运行时获得。

Private Sub Form1_Paint(sender As Object, e as PaintEventArgs) Handles Me.Paint
   Dim X1 as Integer = Button1.Location.X + Button1.Width /2
   Dim Y1 as Integer = Button1.Location.Y + Button1.Height/2
   Dim X2 as Integer = Button2.Location.X + Button2.Width /2
   Dim Y2 as Integer = Button2.location.Y + Button2.Height/2
   Dim X3 as Integer = Button3.Location.X + Button3.Width /2
   Dim Y3 as Integer = Button3.location.Y + Button3.Height/2
   Dim X4 as Integer = Button4.Location.X + Button4.Width /2
   Dim Y4 as Integer = Button4.location.Y + Button4.Height/2
   Dim X5 as Integer = Button5.Location.X + Button5.Width /2
   Dim Y5 as Integer = Button5.location.Y + Button5.Height/2
   Dim X6 as Integer = Button6.Location.X + Button6.Width /2
   Dim Y6 as Integer = Button6.location.Y + Button6.Height/2
   Dim X7 as Integer = Button7.Location.X + Button7.Width /2
   Dim Y7 as Integer = Button7.location.Y + Button7.Height/2
   Dim X8 as Integer = Button8.Location.X + Button8.Width /2
   Dim Y8 as Integer = Button8.location.Y + Button8.Height/2
   Dim X9 as Integer = Button9.Location.X + Button9.Width /2
   Dim Y9 as Integer = Button9.location.Y + Button9.Height/2
   e.Graphics.DrawLine(Pens.Black, X1, Y1, X2, Y2)
   e.Graphics.DrawLine(Pens.Black, X2, Y2, X3, Y3)
   e.Graphics.DrawLine(Pens.Black, X3, Y3, X6, Y6)
End Sub