1

I have a simple widget I'm building, a button that does something when you click it, and something else when you click it again.

I plan on hiding the border via this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;, but when I do this, I lose the ability to move the window around.

How do I allow my button to be moved when the mouse button is held down, and the button to be activated when the mouse button is clicked?

MrDysprosium
  • 489
  • 9
  • 20
  • http://stackoverflow.com/questions/1592876/make-a-borderless-form-movable, http://stackoverflow.com/questions/4767831/drag-borderless-windows-form-by-mouse, http://stackoverflow.com/questions/30184/winforms-click-drag-anywhere-in-the-form-to-move-it-as-if-clicked-in-the-form – Cody Gray - on strike Nov 30 '16 at 13:52

1 Answers1

1

Try this,

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

}
BALA s
  • 169
  • 1
  • 9