1. private void MainForm_Load(object sender, EventArgs e)  
  2. {  
  3.     //绑定事件  
  4.     MouseMove += Form_MouseMove;  
  5.     MouseDown += Form_MouseDown;  
  6. }  
  7. private Point _mousePoint;  
  8. private void Form_MouseMove(object sender, MouseEventArgs e)  
  9. {  
  10.     if (e.Button == MouseButtons.Left)  
  11.     {  
  12.         Top = MousePosition.Y - _mousePoint.Y;  
  13.         Left = MousePosition.X - _mousePoint.X;  
  14.     }  
  15. }  
  16. private void Form_MouseDown(object sender, MouseEventArgs e)  
  17. {  
  18.     if (e.Button == MouseButtons.Left)  
  19.     {  
  20.         _mousePoint.X = e.X;  
  21.         _mousePoint.Y = e.Y;  
  22.     }  
  23. }  

 

如果窗体有标题 
Top -= SystemInformation.CaptionHeight;

如果有边框 
Top -= SystemInformation.FormBorderSize.Height
Left -= SystemInformation.FormBorderSize.Width

相关文章:

  • 2022-01-10
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-27
  • 2021-04-27
  • 2021-08-23
相关资源
相似解决方案