Vb.net在两点之间画线

时间:2012-07-24 13:14:09

标签: vb.net winforms

我想在两点之间画一条线:

http://i.stack.imgur.com/Yuhsd.gif

假设我有 2个面板,并希望从Panel1Panel2画线。

说明:

dim p1 as new panel
dim p2 as new panel

p1.left = 100
p1.top = 10
me.controls.add(p1)

p2.left = 300
p2.top = 20
me.controls.add(p2)

DrawLineBetween(p1,p2)

2 个答案:

答案 0 :(得分:0)

在表单的Paint事件中使用Graphics.DrawLine方法。

答案 1 :(得分:0)

尝试类似下面的内容,它在c#中,但您可以轻松地将其转换为vb.net,并且您需要根据面板位置调整x,y坐标。

   private void Form1_Load(object sender, EventArgs e)
    {
        this.Controls.Add(new Panel{Left = 10, Top = 10,Width = 50,Height = 50, BackColor = Color.Blue});
        this.Controls.Add(new Panel {Left = 100, Top = 100,Width = 50,Height = 50, BackColor = Color.Blue});


    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g;

        g = e.Graphics;

        Pen myPen = new Pen(Color.Red);
        myPen.Width = 1;

        g.DrawLine(myPen, 12, 12, 45, 65);

        g.DrawLine(myPen, 100, 100, 45, 65);


    }