c#界面鼠标拖动

本文介绍了两种实现窗体拖动的方法。第一种方法通过直接操作窗体位置来实现拖动,但存在拖动过程中的残影问题。第二种方法利用了.NET Framework底层API,避免了残影现象,提供了更流畅的用户体验。

 之前做过拖动,找了两种方法:

1、

 private Point myPoint;          private void Form1_MouseMove(object sender, MouseEventArgs e)          {              if (e.Button == MouseButtons.Left)              {                  Point myPosition = Control.MousePosition;                  myPosition.Offset(myPoint.X, myPoint.Y);                  this.DesktopLocation = myPosition;              }          }          private void Form1_MouseUp(object sender, MouseEventArgs e)          {              if (e.Button == MouseButtons.Left)              {                  Point myPosition = Control.MousePosition;                  myPosition.Offset(myPoint.X, myPoint.Y);                  this.DesktopLocation = myPosition;              }          }          private void Form1_MouseDown(object sender, MouseEventArgs e)          {              myPoint = new Point(-e.X, -e.Y);          }        

这种方法要加三个事件Form1_MouseMove,Form1_MouseUp,Form1_MouseDown,可以实现但是拖动过程中有残影。

2、

 [System.Runtime.InteropServices.DllImport("user32.dll")]          public static extern bool ReleaseCapture();          [System.Runtime.InteropServices.DllImport("user32.dll")]          public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);          public const int WM_SYSCOMMAND = 0x0112;          public const int SC_MOVE = 0xF010;          public const int HTCAPTION = 0x0002;          private void Form1_MouseDown(object sender, MouseEventArgs e)          {              try              {                  ReleaseCapture();                  SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);              }              catch                 { }          }

这种方法没有残影。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值