views:

1011

answers:

4

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.

+1  A: 

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.

Matthew Scharley
I figured I would have to use those events but I am not sure what to do with them. When the MouseDown event is called, how do I allow the form to be moved?
Nate Shoffner
On mouse down you set a flag and store the base coordinates.On mouse move - if the flag is set - you adjust the top and left by the offset of the new mouse coordinates.On mouse up you clear the flag.
Murph
Still, you can do this with the Windows API fairly easy which doesn't depend on still getting mouse events. This method does fail if you grab at a pixel at the very top edge of the form and drag upwards, for example.
Joey
+4  A: 

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.

Joey
+3  A: 

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();

Window.DragMove Method (MSDN)

Chris
That's WPF, though. Ok, the OP didn't exactly specify this.
Joey
Yeah, which is something I forgot about the project I was doing. I just looked at Forms and it's not available. Sorry!
Chris
+3  A: 

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);
  }
}
junmats
Seems a bit glitchy..
Nate Shoffner
It is. As said elsewhere already, this relies on the form still generating MouseMove events. As a simple case, suppose you grad the Form at the top-most pixel row and drag upwards. Nothing will happen, although the form will jump around as soon as you move the mouse back onto it.
Joey