Move window without border

2020-02-23 07:46发布

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.

8条回答
啃猪蹄的小仙女
2楼-- · 2020-02-23 08:17

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);
            }
        }
查看更多
smile是对你的礼貌
3楼-- · 2020-02-23 08:17

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;
    }
查看更多
在下西门庆
4楼-- · 2020-02-23 08:17

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

查看更多
家丑人穷心不美
5楼-- · 2020-02-23 08:28

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.

查看更多
成全新的幸福
6楼-- · 2020-02-23 08:32

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.

查看更多
爷、活的狠高调
7楼-- · 2020-02-23 08:37

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.

查看更多
登录 后发表回答