Oct
17
C# Draggable Borderless Forms. Solved!
Filed Under (Code) by Mystalia on 17-10-2008
Ever wonder why a windows form without any FormBorders is not able to be dragged around by the mouse? You’d have thought common sense would dictate that a developer would wan’t his app to be fully functional frames or not, but I guess programming on and for windows means that you have to deal with little annoyances like this with a workaround.
Whack this at the top of the class.
// Form moving
#region
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
#endregion
// Create a new Form1.MouseDown Event in the form designer and put this into it.
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
#region
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
#endregion
// Create a new Form1.MouseDown Event in the form designer and put this into it.
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
This is presuming you want the form background to be the dragging object, but it can be anything really.