原因: 由于 ListView 控件的 Mouse 相关事件无法获取 MouseButtons.Left 属性,从而 无法使用 MouseDown,MouseMove 事件在MouseButtons.Left 的情况下使其移动,
解决:用一个 Timer 对 MouseEventArgs 的 Button 进行检测,替换 ListView 控件的 MouseMove 事件处理
public class Mover { int locX, locY, mpX, mpY; MouseEventArgs mea; Timer timer = new Timer(); ListView lv; public Mover(ListView lv) { this.lv = lv; lv.MouseEnter += _MouseEnter; lv.MouseDown += _MouseDown; lv.MouseUp += _MouseUp; timer.Tick += timer_Tick; } #region public void _MouseDown(object sender, MouseEventArgs e) { mea = e; lv = sender as ListView; //拖动 lv.BringToFront(); lv.Cursor = Cursors.Hand; locX = lv.Location.X; locY = lv.Location.Y; mpX = Control.MousePosition.X;// e.X; mpY = Control.MousePosition.Y; } public void _MouseUp(object sender, MouseEventArgs e) { timer.Stop(); mea = null; lv.Cursor = Cursors.Default; } public void _MouseEnter(object sender, EventArgs e) { timer.Start(); } private void timer_Tick(object sender, EventArgs e) { if (mea == null) return; if (mea.Button == MouseButtons.Left) { locX += Control.MousePosition.X - mpX; locY += Control.MousePosition.Y - mpY; mpX = Control.MousePosition.X; mpY = Control.MousePosition.Y; lv.Location = new Point(locX, locY); } } #endregion }
交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428
注:本人使用过程中暂未发现造成存取不正确的问题
原文:https://www.cnblogs.com/grCod/p/14797762.html