Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag borderless windows form by mouse [duplicate]

Tags:

c#

winforms

drag

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?

like image 689
Javed Akram Avatar asked Jan 22 '11 12:01

Javed Akram


2 Answers

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.

like image 60
Filip Ekberg Avatar answered Sep 23 '22 11:09

Filip Ekberg


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

like image 40
Emond Avatar answered Sep 23 '22 11:09

Emond