C#实现随鼠标移动窗体实例

前端技术 2023/09/08 C#

本文实例讲述了c#实现随鼠标移动窗体的方法,分享给大家供大家参考。

具体实现方法如下:

private void MainForm_Load(object sender, EventArgs e) 
{ 
  //绑定事件 
  MouseMove += Form_MouseMove; 
  MouseDown += Form_MouseDown; 
} 
private Point _mousePoint; 
private void Form_MouseMove(object sender, MouseEventArgs e) 
{ 
  if (e.Button == MouseButtons.Left) 
  { 
    Top = MousePosition.Y - _mousePoint.Y; 
    Left = MousePosition.X - _mousePoint.X; 
  } 
} 
private void Form_MouseDown(object sender, MouseEventArgs e) 
{ 
  if (e.Button == MouseButtons.Left) 
  { 
    _mousePoint.X = e.X; 
    _mousePoint.Y = e.Y; 
  } 
} 

如果窗体有标题则改为:

Top -= SystemInformation.CaptionHeight;

如果有边框则改为:

Top -= SystemInformation.FormBorderSize.Height
Left -= SystemInformation.FormBorderSize.Width

希望本文所述对大家的C#程序设计有所帮助

本文地址:https://www.stayed.cn/item/21669

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。