Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving window by click-drag on a control

I have a WinForms project. I have a panel on the top of my window. I want that panel to be able to move the window, when the user clicks on it and then drags.

How can I do this?

like image 458
Victor Avatar asked Nov 20 '12 16:11

Victor


1 Answers

Add the following declerations to your class:

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;

[DllImport("User32.dll")]
public static extern bool ReleaseCapture();

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

Put this in your panel's MouseDown event:

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}
like image 98
Blachshma Avatar answered Oct 15 '22 04:10

Blachshma