123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System.Drawing;
- using System.Windows.Forms;
- namespace OTSIncAReportApp
- {
- /// <summary>
- /// 拖拽帮助类,控件需要注册该类中的鼠标相关事件
- /// </summary>
- public class DragHelper
- {
- #region 变量定义
- /// <summary>
- /// 光标状态
- /// </summary>
- private enum EnumMousePointPosition
- {
- StateLess, //无状态
- LeftBorderStetch, //左边框拉伸
- TopLeftCornerStretch, //左上角拉伸
- TopBorderStretch, //上边框拉伸
- TopRightCornerStretch, //右上角拉伸
- RightBorderStretch, //右边框拉伸
- BottomRightCornerStretch, //右下角拉伸
- BottomBorderStretch, //下边框拉伸
- BottomLeftCornerStretch, //左下角拉伸
- Drag //拖曳
- }
- /// <summary>
- /// 控件原背景色
- /// </summary>
- private static Color oldColor = default(Color);
- /// <summary>
- /// 控件点击时使用的背景色
- /// </summary>
- private static Color controlFocusedBackcolor = Color.Red;
- /// <summary>
- /// 鼠标拖拽使用的原始点
- /// </summary>
- private static Point mouseDragOldPoint = default(Point);
- /// <summary>
- /// 鼠标上一次改变大小后的位置点
- /// </summary>
- private static Point mouseSizeLastPoint = default(Point);
- /// <summary>
- /// 绘制边框的画笔
- /// </summary>
- private static Pen borderPen = new Pen(Color.Black);
- /// <summary>
- /// 光标变化临界的宽度
- /// </summary>
- private const int BAND = 3;
- /// <summary>
- /// 控件最小宽度
- /// </summary>
- private const int MINWIDTH = 10;
- /// <summary>
- /// 控件最小高度
- /// </summary>
- private const int MINHEIGHT = 10;
- /// <summary>
- /// 当前鼠标操作
- /// </summary>
- private static EnumMousePointPosition mousePointPosition = EnumMousePointPosition.StateLess;
- #endregion
- #region 鼠标相关方法
- /// <summary>
- /// 注册控件拖拽事件
- /// </summary>
- /// <param name="ctl">拖拽区域控件</param>
- public static void RegisterDragEvent(Control ctl)
- {
- ctl.MouseDown += DragHelper.DragControl_MouseDown;
- ctl.MouseUp += DragHelper.DragControl_MouseUp;
- ctl.MouseMove += DragHelper.DragControl_MouseMove;
- }
- /// <summary>
- /// 控件鼠标点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private static void DragControl_MouseDown(object sender, MouseEventArgs e)
- {
- Control currentControl = null;
- mouseDragOldPoint = mouseSizeLastPoint = e.Location; //保存控件当前位置
- currentControl = sender as Control;
- oldColor = currentControl.BackColor; //保存背景色,鼠标弹起时恢复
- currentControl.BackColor = controlFocusedBackcolor; //改变背景色
- }
- /// <summary>
- /// 鼠标在控件上移动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private static void DragControl_MouseMove(object sender, MouseEventArgs e)
- {
- Control currentControl = null;
- currentControl = sender as Control;
- if (e.Button != MouseButtons.Left)
- {
- mousePointPosition = GetMousePointPosition(currentControl.Size, e);
- switch (mousePointPosition)
- {
- case EnumMousePointPosition.LeftBorderStetch:
- case EnumMousePointPosition.RightBorderStretch:
- Cursor.Current = Cursors.SizeWE;
- break;
- case EnumMousePointPosition.TopBorderStretch:
- case EnumMousePointPosition.BottomBorderStretch:
- Cursor.Current = Cursors.SizeNS;
- break;
- case EnumMousePointPosition.TopLeftCornerStretch:
- case EnumMousePointPosition.BottomRightCornerStretch:
- Cursor.Current = Cursors.SizeNWSE;
- break;
- case EnumMousePointPosition.TopRightCornerStretch:
- case EnumMousePointPosition.BottomLeftCornerStretch:
- Cursor.Current = Cursors.SizeNESW;
- break;
- case EnumMousePointPosition.Drag:
- Cursor.Current = Cursors.SizeAll;
- break;
- default:
- Cursor.Current = null;
- break;
- }
- return;
- }
- switch (mousePointPosition)
- {
- case EnumMousePointPosition.Drag:
- currentControl.Left += e.X - mouseDragOldPoint.X;
- currentControl.Top += e.Y - mouseDragOldPoint.Y;
- break;
- case EnumMousePointPosition.BottomBorderStretch:
- currentControl.Height += e.Y - mouseSizeLastPoint.Y;
- mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
- break;
- case EnumMousePointPosition.BottomRightCornerStretch:
- currentControl.Width += e.X - mouseSizeLastPoint.X;
- currentControl.Height = e.Y - mouseSizeLastPoint.Y;
- mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
- break;
- case EnumMousePointPosition.RightBorderStretch:
- currentControl.Width += e.X - mouseSizeLastPoint.X;
- mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
- break;
- case EnumMousePointPosition.TopBorderStretch:
- currentControl.Top += e.Y - mouseDragOldPoint.Y;
- currentControl.Height -= e.Y - mouseDragOldPoint.Y;
- break;
- case EnumMousePointPosition.LeftBorderStetch:
- currentControl.Left += e.X - mouseDragOldPoint.X;
- currentControl.Width -= e.X - mouseDragOldPoint.X;
- break;
- case EnumMousePointPosition.BottomLeftCornerStretch:
- currentControl.Left += e.X - mouseDragOldPoint.X;
- currentControl.Width -= e.X - mouseDragOldPoint.X;
- currentControl.Height += e.Y - mouseSizeLastPoint.Y;
- mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
- break;
- case EnumMousePointPosition.TopRightCornerStretch:
- currentControl.Top += e.Y - mouseDragOldPoint.Y;
- currentControl.Width += e.X - mouseSizeLastPoint.X;
- currentControl.Height -= e.Y - mouseDragOldPoint.Y;
- mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
- break;
- case EnumMousePointPosition.TopLeftCornerStretch:
- currentControl.Left += e.X - mouseDragOldPoint.X;
- currentControl.Top += e.Y - mouseDragOldPoint.Y;
- currentControl.Width -= e.X - mouseDragOldPoint.X;
- currentControl.Height -= e.Y - mouseDragOldPoint.Y;
- break;
- default: break;
- }
- if (currentControl.Width < MINWIDTH)
- {
- currentControl.Width = MINWIDTH;
- }
- if (currentControl.Height < MINHEIGHT)
- {
- currentControl.Height = MINHEIGHT;
- }
- }
- /// <summary>
- /// 控件鼠标弹起事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private static void DragControl_MouseUp(object sender, MouseEventArgs e)
- {
- Control currentControl = null;
- currentControl = sender as Control;
- Cursor.Current = null;
- mousePointPosition = EnumMousePointPosition.StateLess;
- currentControl.BackColor = oldColor; //恢复控件背景色
- }
- /// <summary>
- /// 获取鼠标位置
- /// </summary>
- /// <param name="size">控件大小</param>
- /// <param name="e">鼠标位置</param>
- /// <returns>鼠标光标位置</returns>
- private static EnumMousePointPosition GetMousePointPosition(Size size, MouseEventArgs e)
- {
- if ((e.X < (-1 * BAND)) || (e.X > size.Width) || (e.Y < (-1 * BAND)) || (e.Y > size.Height))//光标越界
- {
- return EnumMousePointPosition.StateLess;
- }
- if (e.X < BAND)
- {
- if (e.Y < BAND)
- {
- return EnumMousePointPosition.TopLeftCornerStretch;
- }
- else
- {
- if (e.Y > ((-1 * BAND) + size.Height))
- {
- return EnumMousePointPosition.BottomLeftCornerStretch;
- }
- else
- {
- return EnumMousePointPosition.LeftBorderStetch;
- }
- }
- }
- else
- {
- if (e.X > (-1 * BAND + size.Width))
- {
- if (e.Y < BAND)
- {
- return EnumMousePointPosition.TopRightCornerStretch;
- }
- else
- {
- if (e.Y > (-1 * BAND + size.Height))
- {
- return EnumMousePointPosition.BottomRightCornerStretch;
- }
- else
- {
- return EnumMousePointPosition.RightBorderStretch;
- }
- }
- }
- else
- {
- if (e.Y < BAND)
- {
- return EnumMousePointPosition.TopBorderStretch;
- }
- else
- {
- if (e.Y > (-1 * BAND + size.Height))
- {
- return EnumMousePointPosition.BottomBorderStretch;
- }
- else
- {
- return EnumMousePointPosition.Drag;
- }
- }
- }
- }
- }
- #endregion
- #region 绘制相关
- /// <summary>
- /// 绘制控件边框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private static void DragControlParent_Paint(object sender, PaintEventArgs e)
- {
- Control currentControl = null;
- currentControl = sender as Control;
- return;
- }
- #endregion
- }
- }
|