Move window without border
Asked Answered
G

8

18

How do I move a window that does not have a border. There is no empty space on the application, all that is available is a webbrowser and a menustrip. I would like the users to be able to move the window by dragging the menu strip. How do I code this? I have tried a few code blocks I have found online, but none of them worked.

Groundmass answered 2/1, 2011 at 4:29 Comment(1)
Probably because it's impossible for the application (or the user) to distinguish whether a click on the MenuStrip is intended to move the application or open a menu. There's a reason that windows have borders—rethink your design.Gaskill
D
38

This Code Project article should help you accomplish this. I've used this myself with no problems. This is the jist of it:

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 will basically "trick" the window manager into thinking that it is grabbing the title bar of the winform.

To apply it to your project, just use the MouseDown event from the MenuStrip.

Daciadacie answered 2/1, 2011 at 22:25 Comment(2)
That works really great. You can add the contents of the method in any MouseDown event, for example a picture...Karyosome
This works even if a borderless form is run inside another c# app where the Point method fails. Thank you.Linzer
R
24

Here is the .Net Way

    private bool dragging = false;
    private Point dragCursorPoint;
    private Point dragFormPoint;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;
        dragCursorPoint = Cursor.Position;
        dragFormPoint = this.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
            this.Location = Point.Add(dragFormPoint, new Size(dif));
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

that's it.

Raquelraquela answered 14/10, 2013 at 9:25 Comment(1)
waow. perfect .net solution. thank you so muchYeast
D
2

Just put the start point into an 2D Array like this:

public partial class mainForm : Form
{
    //Global variables for Moving a Borderless Form
    private bool dragging = false;
    private Point startPoint = new Point(0, 0); 


    public mainForm()
    {
        InitializeComponent();
    }

    private void mainForm_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;
        startPoint = new Point(e.X, e.Y);

    }

    private void mainForm_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

    private void mainForm_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            Point p = PointToScreen(e.Location);
            Location = new Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);

        }

    }
}
Drysalt answered 22/2, 2017 at 12:52 Comment(0)
S
0

You can fake your menustrip, for example using a panel with a label instead. And then you can handle this manually: when the user clicks the label, a popup menu will open, and when the user drags the label, the window will move. But I would advise against such workarounds, because it's not a standard GUI behavior, and you might get your users confused.

Spokane answered 2/1, 2011 at 5:31 Comment(3)
Please don't do this. Not only is it a completely non-standard user interface, but it still doesn't solve the problem of interpreting a click. Remember that a "drag" happens after a click occurs. Should a click open the menu, or should it be interpreted as beginning a drag? Time machines haven't been perfected yet.Gaskill
@Cody A click happens when you release the mouse button. Look how it works in the Folders pane of Windows Explorer: if you click a folder, it opens in the right pane, but if you drag a folder, it doesn't open, it just gets dragged.Spokane
Understood. But understand that you're defining a "Click" event differently from how a menu control interprets it in Windows. As soon as I press down the mouse button on any menu in any application in Windows, the menu drops down. It doesn't occur when the mouse button is released. I think it's important to point out that design decisions that dramatically alter expected behavior need to be considered especially carefully.Gaskill
C
0

I haven't tried it, but if you can handle the "OnMouseDown" and "onMouseUp" events on the menu bar:

  • On mouse down - Move the window according to the mouse movement
  • Stop tracking the mouse movement on mouse up, or mouse out
Credible answered 2/1, 2011 at 22:13 Comment(0)
H
0

If you are using a Panel you have to add this in the

YourForm.Designer.cs

this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);

and this in the

YourForm.cs

 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 panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
Hunterhunting answered 26/7, 2013 at 7:18 Comment(0)
B
0

Mbithi Kioko is on the right track but i would do it this way.

    bool dragging = false;
    int xOffset = 0;
    int yOffset = 0;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;

        xOffset = Cursor.Position.X - this.Location.X;
        yOffset = Cursor.Position.Y - this.Location.Y;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            this.Location = new Point(Cursor.Position.X - xOffset, Cursor.Position.Y - yOffset);
            this.Update();
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }
Bullnecked answered 11/12, 2013 at 18:23 Comment(0)
A
0

I had to use System.Runtime.InteropServices.DllImportAttribute - just thought I would comment and let you all know.

Acetate answered 7/1, 2017 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.