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
Photoshop Tutorial : How to Apply a Weather Effect to Your Text ?This tutorial will show you how to add a Weather effect to your text , weather snow or any thing you want to add a look like a natural effect .
Photoshop Tutorial: Changing the Color Tone of Your PhotosAn easy step-by-step tutorial on how to tone your pictures black and white and different colors. This will surely advance your creativity and broaden your basic Photoshop skills.- A Basic Introduction on Filing Form 1040This article gives a basic run through on how to fill out form 1040 by yourself.
- Helping Elderly Parents Make Their Last Move This article has information regarding helping elderly parents to make their last move a smooth and enjoyable transition.
Top 10 Things to Do Before a Military MoveA Military move is much more than just moving down the street. It often means moving to another country or hemisphere. Make sure you have your ducks in a row before you go.
- Thompson Raceway Park, Drag Racing Fun for Everyone
- Packing to Move Back Home from College
- Fantasy Baseball Rankings: Top Fantasy Hitters to Move Up
- Ten Reasons Western Expats Move to Thailand: It's Not Only About the Thai Girls!
- Microsoft Office Word 2007 - How to Insert, Select, Delete, Move, and Copy Text
- A New Form of the Common Cold
- Tutorial: How to Sabotage Yourself




4 Comments
Post a CommentHey, thanks for the tuto, im unable to implement this on a wpf form, is there any way to manage that?
lamin
Great !! Good job it's better than usins events mouseDown and mouseMove of the form !
Thx !!
Works fine on a Visual C# project !
This tutorial is for Visual CSharp .Net, not for VB .Net.
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