I am going to describe two method of moving Window Form without title bar.
I Method – Using Windows API
Add following code in your .cs file
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
Now write following code on MouseMove event of that control on which window form should move –
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
II Method : Using C# Code
Add following variables in your .cs file.
private bool mouseIsDown = false;
private Point firstPoint;
Now handle MouseMove, MouseUp, MouseDown event of that window control on which window form should move.
Code is as follow –
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
// Get the difference between the two points
int xDiff = firstPoint.X - e.Location.X;
int yDiff = firstPoint.Y - e.Location.Y;
// Set the new point
int x = this.Location.X - xDiff;
int y = this.Location.Y - yDiff;
this.Location = new Point(x, y);
}
}
private void panel1_MouseDown_1(object sender, MouseEventArgs e)
{
firstPoint = e.Location;
mouseIsDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
Note: Drawback of second method is, when cursor will move below taskbar then window form will also go below taskbar.