65.9K
CodeProject is changing. Read more.
Home

Dragging a Borderless Form

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (9 votes)

May 31, 2010

CPOL
viewsIcon

32192

A code snippet which makes a borderless form draggable.

I was looking through some old C# projects on my external hard drive, when I stumbled upon the following code snippet, which makes a borderless form draggable.
protected override void WndProc(ref Message m)
{
    if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
        m.WParam = (IntPtr)1;
    base.WndProc(ref m);
    if (m.Msg == 132 && m.Result.ToInt32() == 1)
        m.Result = (IntPtr)2;
}
This makes the form draggable by clicking and dragging anywhere on the form itself (that is, not on controls unless they are disabled).