C# Windows 按行形成两个按钮 Link

C# Windows Forms Link two buttons by line

嘿,我有两个按钮(图 1),我想按行 link 这个按钮。我想过用 Class Graphics and Pen 来画线,但我试过了,但没用。

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            var lineBegin = new Point(button1.Left + button1.Width - 1, button1.Top + button1.Height / 2);
            var lineEnd = new Point(button2.Left, button2.Top + button2.Height / 2);

            e.Graphics.DrawLine(Pens.Maroon, lineBegin, lineEnd);
        }

这个有效

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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

        e.Graphics.DrawLine(
            Pens.Blue,
            button1.Right + 2,
            button1.Top + button1.Height / 2,
            button2.Left - 2,
            button2.Top + button2.Height / 2);
    }
}