Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dragging a Borderless Form

0.00/5 (No votes)
13 Jul 2010 2  
A slightly different solution.using System.Runtime.InteropServices;private const int WM_NCLBUTTONDOWN = 0xA1;private const int HTCAPTION = 0x2;[DllImport(User32.dll)]private static extern bool ReleaseCapture();[DllImport(User32.dll)]private static extern int...
A slightly different solution.

C#
using System.Runtime.InteropServices;

C#
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;

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

public Form()
{
    InitializeComponent();
    AssignHandler(this);
}

private void AssignHandler(Control control)
{
    if (control is Form
        || control is Label
        || control is Panel
        || control is PictureBox
        || control is TableLayoutPanel)
    {
        control.MouseDown += Form_MouseDown;
    }
    foreach (Control subControl in control.Controls)
    {
        AssignHandler(subControl);
    }
}

private void Form_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here