how to make a panel draggable in c#

how to make a panel draggable in c#

You can create movable panels when you create c# winForm.

I told you how to do it in detail. Look carefully and follow along.

If you have any questions, please feel free to email us.


1. Copy the source code below to the top of your code.

[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);

private const int SYSTEMCOMMAND = 0x112;
private const int SC_DRAGMOVE = 0xF012;

// This function moves the MainForm.
private void panel_MouseMove(object sender, MouseEventArgs e)
SendMessage(this.Handle, SYSTEMCOMMAND, SC_DRAGMOVE, 0);

// This function only moves the panel.
private void panel_MouseMove_object(object sender, MouseEventArgs e)

Panel panel = sender as Panel;
SendMessage(panel.Handle, SYSTEMCOMMAND, SC_DRAGMOVE, 0);


2. Add ‘MouseMove events’ to the panel you want to move.

Select a mousemove event from a mouse event entry in the visual studio.

 this.panel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_MouseMove_object);



3. Move the panel.

If it works well, it’s a success.


thank you 🙂