如何使我的简单Windows窗体按钮“单击并拖动”?

时间:2016-11-30 13:51:39

标签: c# winforms

我正在构建一个简单的小部件,一个按钮,当您单击它时会执行某些操作,而当您再次单击它时会执行其他操作。

我打算通过this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;隐藏边框,但是当我这样做时,我失去了移动窗口的能力。

按住鼠标按钮时如何允许移动按钮,单击鼠标按钮时要激活按钮?

1 个答案:

答案 0 :(得分:1)

试试这个,

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


    private Point MouseDownLocation;


    private void btn_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void btn_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            btn.Left = e.X + btn.Left - MouseDownLocation.X;
            btn.Top = e.Y + btn.Top - MouseDownLocation.Y;
        }
    }

}