You want to move a window without a Titlebar, so you have to load the required DLL's to the project. ReleaseCapture
: removes mouse capture from the object in the current document, and SendMessage
: sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.
You can use the following code to move your form with the MouseDown event:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
private void MouseDownEvent(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
Remember, you can add MouseDownEvent to any control on your form. For example, if you have a label on your form, you also can add MouseDownEvent()
to the label's MouseDown
Event too
As you can see in the InitializeComponent()
method, I added MouseDownEvent
to both Form1
and lable1
:
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MouseDownEvent);
//
// Form1
//
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MouseDownEvent);
}