2016-11-30 44 views
1

我有一个简单的小部件,我正在构建,一个按钮,当你点击它时会做某些事情,当你再次点击它时会出现其他的东西。如何让我简单的Windows窗体按钮“点击并拖动”?

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

当鼠标按钮被按下时,如何让我的按钮被移动,点击鼠标按钮时按钮被激活?

+0

http://stackoverflow.com/questions/1592876/make-a-borderless-form-movable,http://stackoverflow.com/questions/4767831/drag-borderless -windows-form-b​​y-mouse,http://stackoverflow.com/questions/30184/winforms-click-drag-anywhere-in-the-form-to-move-it-as-if-clicked-in-the -form –

回答

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; 
     } 
    } 

} 
+0

哈,好简单。我喜欢!谢谢一堆。 – MrDysprosium

+0

如果窗口在'mouseup'之前移动,任何简单的方法来停止按钮被“点击”? – MrDysprosium

+0

或者,您必须使用“CTRL + MOUSE_DOWN”或“SHIFT + MOUSE_DOWN”,释放键盘按钮或鼠标按钮时,它将停止拖动。 –