CSharp .Net Tutorial: Move a Borderless Form

James Cloud
Borderless forms are quite useful in CSharp .Net Window's Form applications, as they allow for a more customized and skinned look for your application. However, when a form's border property is set to borderless some limitations are applied and set in place. For example, the user can no longer drag and move the form anywhere on the screen by simply dragging it.

This tutorial will show you an easy way to let the user drag and move a borderless form any where on the screen.

Dragging and Moving the Borderless Windows Form

In order to allow the user to move a borderless form you will need to intercept the form's Window Process Messages, or WndProc for short. To intercept Window Process Messages you will need to override your form's built in WndProc function. To do this just make a protected override void called WndProc. After you've created the protected void simply add this code to it:

protected override void WndProc(ref Message WndProcMsg)
{

const int CHITTEST = 0x0084;
const int CAPTIONHT = 2;

switch (WndProcMsg.Msg)
{
case CHITTEST:
{

Point PNT = this.PointToClient(new Point(WndProcMsg.LParam.ToInt32()));

if (ClientRectangle.Contains(PNT ))
{
WndProcMsg.Result = new IntPtr(CAPTIONHT);
return;
}
break;
}

}
base.WndProc(ref WndProcMsg);
}

Now make your form borderless by either setting the FormBorderStyle property to None or by execute this code, this.FormBorderStyle = FormBorderStyle.None;.

Conclusion

Whenever the user clicks and drag any part of the form's client area the form will move it's position according to where ever the user drops it.

You can apply this to any form in your project by simply using the WndProc method demonstrated with the source code found above. You can also use this code with a form that has a border, allowing the user to move the form with the border and by dragging the client area.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

4 Comments

Post a Comment
  • Andreas Oxinos2/27/2010

    Hey, thanks for the tuto, im unable to implement this on a wpf form, is there any way to manage that?
    lamin

  • Guillaume8/6/2009

    Great !! Good job it's better than usins events mouseDown and mouseMove of the form !

    Thx !!

    Works fine on a Visual C# project !

  • James Cloud7/31/2009

    This tutorial is for Visual CSharp .Net, not for VB .Net.

  • JM7/31/2009

    It doesn't work for me.
    I created the following in VB.NET:

    Private Const WM_NCHITTEST As Integer = &H84
    Private Const HTCLIENT As Integer = &H1
    Private Const HTCAPTION As Integer = &H2

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Select Case m.Msg
    Case WM_NCHITTEST
    'Dim p As Point = Me.PointToClient(New Point(m.LParam.ToInt32()))
    'If ClientRectangle.Contains(p) Then
    ' m.Result = New IntPtr(HTCAPTION)
    'End If

    If (m.Result.ToInt32 = HTCLIENT) Then
    m.Result = IntPtr.op_Explicit(HTCAPTION)
    Exit Sub
    End If
    End Select

    MyBase.WndProc(m)
    End Sub

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.