Is there a way to make a form that has no border (FormBorderStyle is set to "none") movable when the mouse is clicked down on the form just as if there was a border? Thanks.
There's no property you can flip to make this just happen magically. Look at the events for the form and it becomes fairly trivial to implement this by setting this.Top
and this.Left
. Specifically you'll want to look at MouseDown
, MouseUp
and MouseMove
.
This article on CodeProject details a technique. Is basically boils down to:
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();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
This essentially does exactly the same as grabbing the title bar of a window, from the window manager's point of view.
don't have the exact code to hand, but in a recent project I think I used MouseDown event and simply put this:
frmBorderless.DragMove();
use MouseDown, MouseMove and MouseUp. You can set a variable flag for that. I have a sample, but I think you need to revise.
I am coding the mouse action to a panel. Once you click the panel, your form will move with it.
//Global variables;
Private bool Dragging = false;
Private Point offset;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Dragging = true; // Dragging is your variable flag
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(Dragging)
{
Point currentPos = PointToScreen(e.Location);
Location = new Point(currentPos.X - offset.X, currentPos.Y - offset.Y);
}
}