Make a borderless form movable?

前端 未结 21 2158
情歌与酒
情歌与酒 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:38

    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 Point _start_point=new Point(0,0);
    
    
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
       _dragging = true;  // _dragging is your variable flag
       _start_point = new Point(e.X, e.Y);
    }
    
    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
       _dragging = false; 
    }
    
    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
      if(_dragging)
      {
         Point p = PointToScreen(e.Location);
         Location = new Point(p.X - this._start_point.X,p.Y - this._start_point.Y);     
      }
    }
    

提交回复
热议问题