class MoveAccessPoint { bool IsMoving = false; Point pCtrlLastCoordinate = new Point(0, 0); Point pCursorOffset = new Point(0, 0); Point pCursorLastCoordinate = new Point(0, 0); private Control ctrl = null; private ScrollableControl Containe = null; #region 私有方法 /// <summary> /// 在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置 /// </summary> private void DeviceMouseDown(object sender, MouseEventArgs e) { Button currentButton = (Button)sender; if (Containe == null) { return; } if (e.Button == MouseButtons.Left) { IsMoving = true; pCtrlLastCoordinate.X = currentButton.Left; pCtrlLastCoordinate.Y = currentButton.Top; pCursorLastCoordinate.X = Cursor.Position.X; pCursorLastCoordinate.Y = Cursor.Position.Y; } } private void DeviceMouseMove(object sender, MouseEventArgs e) { Button currentButton = (Button)sender; if (Containe == null) { return; } if (e.Button == MouseButtons.Left) { if (this.IsMoving) { Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y); //Point pCursor = Containe.PointToClient(Cursor.Position); pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X; pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y; currentButton.Left = pCtrlLastCoordinate.X + pCursorOffset.X; currentButton.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y; //修正参数 if (currentButton.Left < 0) currentButton.Left = 0; if (currentButton.Top < 0) currentButton.Top = 0; if (currentButton.Left + currentButton.Width > this.Containe.HorizontalScroll.Maximum) currentButton.Left = this.Containe.HorizontalScroll.Maximum - currentButton.Width; if (currentButton.Top + currentButton.Height > this.Containe.VerticalScroll.Maximum) currentButton.Top = this.Containe.VerticalScroll.Maximum - currentButton.Height; } } } private void DeviceMouseUp(object sender, MouseEventArgs e) { Button currentButton = (Button)sender; if (Containe == null) { return; } if (this.IsMoving) { pCursorOffset.X = 0; pCursorOffset.Y = 0; IsMoving = false; } } #endregion public void EditMode(Control c, ScrollableControl parentContain) { ctrl = c; this.Containe = parentContain; ctrl.MouseDown += new MouseEventHandler(DeviceMouseDown); ctrl.MouseMove += new MouseEventHandler(DeviceMouseMove); ctrl.MouseUp += new MouseEventHandler(DeviceMouseUp); } public void SaveMode(Control c, ScrollableControl parentContain) { ctrl = c; this.Containe = parentContain; ctrl.MouseDown -= new MouseEventHandler(DeviceMouseDown); ctrl.MouseMove -= new MouseEventHandler(DeviceMouseMove); ctrl.MouseUp -= new MouseEventHandler(DeviceMouseUp); } public void simulateDeviceMouseDown(Point lpPoint) { Win32.simulateDeviceMouseDown( lpPoint); } }