Make a borderless form movable?

前端 未结 21 1819
情歌与酒
情歌与酒 2020-11-22 09:48

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?

相关标签:
21条回答
  • 2020-11-22 10:22

    I'm expanding the solution from jay_t55 with one more method ToolStrip1_MouseLeave that handles the event of the mouse moving quickly and leaving the region.

    private bool mouseDown;
    private Point lastLocation;
    
    private void ToolStrip1_MouseDown(object sender, MouseEventArgs e) {
        mouseDown = true;
        lastLocation = e.Location;
    }
    
    private void ToolStrip1_MouseMove(object sender, MouseEventArgs e) {
        if (mouseDown) {
            this.Location = new Point(
                (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
    
            this.Update();
        }
    }
    
    private void ToolStrip1_MouseUp(object sender, MouseEventArgs e) {
        mouseDown = false;
    }
    
    private void ToolStrip1_MouseLeave(object sender, EventArgs e) {
        mouseDown = false;
    }
    
    0 讨论(0)
  • 2020-11-22 10:23

    Let's not make things any more difficult than they need to be. I've come across so many snippets of code that allow you to drag a form around (or another Control). And many of them have their own drawbacks/side effects. Especially those ones where they trick Windows into thinking that a Control on a form is the actual form.

    That being said, here is my snippet. I use it all the time. I'd also like to note that you should not use this.Invalidate(); as others like to do because it causes the form to flicker in some cases. And in some cases so does this.Refresh. Using this.Update, I have not had any flickering issues:

    private bool mouseDown;
    private Point lastLocation;
    
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            lastLocation = e.Location;
        }
    
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouseDown)
            {
                this.Location = new Point(
                    (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
    
                this.Update();
            }
        }
    
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }
    
    0 讨论(0)
  • 2020-11-22 10:23

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/d803d869-68e6-46ff-9ff1-fabf78d6393c/how-to-make-a-borderless-form-in-c?forum=csharpgeneral

    This bit of code from the above link did the trick in my case :)

    protected override void OnMouseDown(MouseEventArgs e)  
    
    {
          base.OnMouseDown(e);
          if (e.Button == MouseButtons.Left)
          {
            this.Capture = false;
            Message msg = Message.Create(this.Handle, 0XA1, new IntPtr(2), IntPtr.Zero);
            this.WndProc(ref msg);
          }
    }
    
    0 讨论(0)
  • 2020-11-22 10:23

    For .NET Framework 4,

    You can use this.DragMove() for the MouseDown event of the component (mainLayout in this example) you are using to drag.

    private void mainLayout_MouseDown(object sender, MouseButtonEventArgs e)
    {
        this.DragMove();
    }
    
    0 讨论(0)
  • 2020-11-22 10:25

    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.

    0 讨论(0)
  • 2020-11-22 10:25

    Since some answers do not allow for child controls to be draggable, I've created a little helper class. It should be passed the top level form. Can be made more generic if desired.

    class MouseDragger
    {
        private readonly Form _form;
        private Point _mouseDown;
    
        protected void OnMouseDown(object sender, MouseEventArgs e)
        {
            _mouseDown = e.Location;
        }
    
        protected void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int dx = e.Location.X - _mouseDown.X;
                int dy = e.Location.Y - _mouseDown.Y;
                _form.Location = new Point(_form.Location.X + dx, _form.Location.Y + dy);
            }
        }
        public MouseDragger(Form form)
        {
            _form = form;
    
            MakeDraggable(_form);            
        }
    
        private void MakeDraggable(Control control)
        {
            var type = control.GetType();
            if (typeof(Button).IsAssignableFrom(type))
            {
                return;
            }
    
            control.MouseDown += OnMouseDown;
            control.MouseMove += OnMouseMove;
    
            foreach (Control child in control.Controls)
            {
                MakeDraggable(child);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题