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:38

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

        }

    }
}
查看更多
The star\"
3楼-- · 2020-02-23 08:40

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
查看更多
登录 后发表回答