C# 实现拖拉控件改变位置与大小的方法

2019-12-30 19:13:45于丽

接着我们需要实现MouseDown、MouseMove、MouseUp三个事件。

不过在此之前,我们必须要弄清楚,移动即表示坐标的改变,所以必定要有个起始坐标和终点坐标。

所以我们在MoveControl类中加入两个字段。

private Point pPoint; //上个鼠标坐标 private Point cPoint; //当前鼠标坐标

而且在开始拖动之前,我们肯定需要先单击一次控件。在MouseDown时获取当前光标的位置,保存到pPoint中。

(此处用Cursor获得坐标的好处,就是忽略掉容器的麻烦问题)


/// <summary>
/// 鼠标单击事件:用来显示边框
/// </summary>
 void MouseClick(object sender, MouseEventArgs e)
 {
  pPoint = Cursor.Position; 
 }
 

接着便实现MouseMove的事件,当鼠标左键按下时,接着移动鼠标后,继续鼠标移动后的坐标,然后与MouseDown时记下的坐标相减,就得到鼠标的位移值,接着控件的Location加上该位移值即可,然后更新pPoint。  


/// <summary>
  /// 鼠标移动事件:让控件跟着鼠标移动
  /// </summary>
  void MouseMove(object sender, MouseEventArgs e)
  {
   Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
   //当鼠标左键按下时才触发
   if (e.Button == MouseButtons.Left)
   {
    cPoint = Cursor.Position; //获得当前鼠标位置
    int x = cPoint.X - pPoint.X;
    int y = cPoint.Y - pPoint.Y;
    currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
    pPoint = cPoint;
   }
  }

由于此时还没涉及到边框,所以MouseUp暂时不用处理。至此拖动的基本功能已经实现!

目前MoveControl的完整代码如下:

MoveControl


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace DragControl
{
 public class MoveControl
 {
  #region Constructors
  public MoveControl(Control ctrl)
  {
   currentControl = ctrl;
   AddEvents();
  }
  #endregion
  #region Fields
  private Control currentControl; //传入的控件
  private Point pPoint; //上个鼠标坐标
  private Point cPoint; //当前鼠标坐标
  #endregion
  #region Properties
  #endregion
  #region Methods
  /// <summary>
  /// 挂载事件
  /// </summary>
  private void AddEvents()
  {
   currentControl.MouseDown += new MouseEventHandler(MouseDown);
   currentControl.MouseMove += new MouseEventHandler(MouseMove);
   currentControl.MouseUp += new MouseEventHandler(MouseUp);
  }
  /// <summary>
  /// 绘制拖拉时的黑色边框
  /// </summary>
  public static void DrawDragBound(Control ctrl)
  {
   ctrl.Refresh();
   Graphics g = ctrl.CreateGraphics();
   int width = ctrl.Width;
   int height = ctrl.Height;
   Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0),
    new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
   g.DrawLines(new Pen(Color.Black), ps);
  } 
  #endregion
  #region Events
  /// <summary>
  /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
  /// </summary>
  void MouseDown(object sender, MouseEventArgs e)
  {
   pPoint = Cursor.Position;
  }
  /// <summary>
  /// 鼠标移动事件:让控件跟着鼠标移动
  /// </summary>
  void MouseMove(object sender, MouseEventArgs e)
  {
   Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
   //当鼠标左键按下时才触发
   if (e.Button == MouseButtons.Left)
   {
    MoveControl.DrawDragBound(this.currentControl);
    cPoint = Cursor.Position; //获得当前鼠标位置
    int x = cPoint.X - pPoint.X;
    int y = cPoint.Y - pPoint.Y;
    currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
    pPoint = cPoint;
   }
  }
  /// <summary>
  /// 鼠标弹起事件:让自定义的边框出现
  /// </summary>
  void MouseUp(object sender, MouseEventArgs e)
  {
   this.currentControl.Refresh();
  }
  #endregion
 }
}