6

Possible Duplicate:
C# - Make a borderless form movable?

I have made a form without border in C#, by setting

this.FormBorderStyle = FormBorderStyle.None;

Now, problem is how can I drag it by mouse?

Community
  • 1
  • 1
Javed Akram
  • 15,024
  • 26
  • 81
  • 118

2 Answers2

23

This should be what you are looking for "Enhanced: Drag and move WinForms"

public partial class MyDraggableForm : Form
{
    private const int WM_NCHITTEST = 0x84;
    private const int HTCLIENT = 0x1;
    private const int HTCAPTION = 0x2;

    ///
    /// Handling the window messages
    ///
    protected override void WndProc(ref Message message)
    {
        base.WndProc(ref message);

        if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
            message.Result = (IntPtr)HTCAPTION;
    }
    public MyDraggableForm()
    {
        InitializeComponent();
    }
}

As the blog post states, this is a way to "fool" the system. This way you don't need to think about mouse up/down events.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • 3
    Keep in mind that as a side effect the window will maximize window on double click. [This answer](http://stackoverflow.com/a/1592899/855432) does not cause this problem. – ghord Jul 26 '13 at 07:37
  • Can I make it draggable on certain portion only? eg withing a control region – Thunder Jul 12 '14 at 03:10
  • It also kills the right mouse button. – TaW Oct 10 '14 at 08:09
  • I'm sorry, but this is a horrible solution. It kills right-click, double-clicking anywhere on the form maximizes it... It's already more trouble than it's worth - could've already implemented mouseDown/mouseUp and mouseMove events by the time you finish fixing the maximize and right-click-killer issues. – SE13013 Jul 17 '15 at 03:23
  • FYI, link in answer is dead - *"jachman.wordpress.com is no longer available. The authors have deleted this site."* – Pang Jun 24 '17 at 01:48
2

You have to register for the MouseDown, MouseUp and MouseMove events and move the form according to the movement of the mouse.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • 1
    This answer would be more valuable with a code example. The math for moving the form based on mouse coordinates is not trivial. – servermanfail Feb 28 '17 at 12:21