DragHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace OTSIncAReportApp
  4. {
  5. /// <summary>
  6. /// 拖拽帮助类,控件需要注册该类中的鼠标相关事件
  7. /// </summary>
  8. public class DragHelper
  9. {
  10. #region 变量定义
  11. /// <summary>
  12. /// 光标状态
  13. /// </summary>
  14. private enum EnumMousePointPosition
  15. {
  16. StateLess, //无状态
  17. LeftBorderStetch, //左边框拉伸
  18. TopLeftCornerStretch, //左上角拉伸
  19. TopBorderStretch, //上边框拉伸
  20. TopRightCornerStretch, //右上角拉伸
  21. RightBorderStretch, //右边框拉伸
  22. BottomRightCornerStretch, //右下角拉伸
  23. BottomBorderStretch, //下边框拉伸
  24. BottomLeftCornerStretch, //左下角拉伸
  25. Drag //拖曳
  26. }
  27. /// <summary>
  28. /// 控件原背景色
  29. /// </summary>
  30. private static Color oldColor = default(Color);
  31. /// <summary>
  32. /// 控件点击时使用的背景色
  33. /// </summary>
  34. private static Color controlFocusedBackcolor = Color.Red;
  35. /// <summary>
  36. /// 鼠标拖拽使用的原始点
  37. /// </summary>
  38. private static Point mouseDragOldPoint = default(Point);
  39. /// <summary>
  40. /// 鼠标上一次改变大小后的位置点
  41. /// </summary>
  42. private static Point mouseSizeLastPoint = default(Point);
  43. /// <summary>
  44. /// 绘制边框的画笔
  45. /// </summary>
  46. private static Pen borderPen = new Pen(Color.Black);
  47. /// <summary>
  48. /// 光标变化临界的宽度
  49. /// </summary>
  50. private const int BAND = 3;
  51. /// <summary>
  52. /// 控件最小宽度
  53. /// </summary>
  54. private const int MINWIDTH = 10;
  55. /// <summary>
  56. /// 控件最小高度
  57. /// </summary>
  58. private const int MINHEIGHT = 10;
  59. /// <summary>
  60. /// 当前鼠标操作
  61. /// </summary>
  62. private static EnumMousePointPosition mousePointPosition = EnumMousePointPosition.StateLess;
  63. #endregion
  64. #region 鼠标相关方法
  65. /// <summary>
  66. /// 注册控件拖拽事件
  67. /// </summary>
  68. /// <param name="ctl">拖拽区域控件</param>
  69. public static void RegisterDragEvent(Control ctl)
  70. {
  71. ctl.MouseDown += DragHelper.DragControl_MouseDown;
  72. ctl.MouseUp += DragHelper.DragControl_MouseUp;
  73. ctl.MouseMove += DragHelper.DragControl_MouseMove;
  74. }
  75. /// <summary>
  76. /// 控件鼠标点击事件
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private static void DragControl_MouseDown(object sender, MouseEventArgs e)
  81. {
  82. Control currentControl = null;
  83. mouseDragOldPoint = mouseSizeLastPoint = e.Location; //保存控件当前位置
  84. currentControl = sender as Control;
  85. oldColor = currentControl.BackColor; //保存背景色,鼠标弹起时恢复
  86. currentControl.BackColor = controlFocusedBackcolor; //改变背景色
  87. }
  88. /// <summary>
  89. /// 鼠标在控件上移动事件
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private static void DragControl_MouseMove(object sender, MouseEventArgs e)
  94. {
  95. Control currentControl = null;
  96. currentControl = sender as Control;
  97. if (e.Button != MouseButtons.Left)
  98. {
  99. mousePointPosition = GetMousePointPosition(currentControl.Size, e);
  100. switch (mousePointPosition)
  101. {
  102. case EnumMousePointPosition.LeftBorderStetch:
  103. case EnumMousePointPosition.RightBorderStretch:
  104. Cursor.Current = Cursors.SizeWE;
  105. break;
  106. case EnumMousePointPosition.TopBorderStretch:
  107. case EnumMousePointPosition.BottomBorderStretch:
  108. Cursor.Current = Cursors.SizeNS;
  109. break;
  110. case EnumMousePointPosition.TopLeftCornerStretch:
  111. case EnumMousePointPosition.BottomRightCornerStretch:
  112. Cursor.Current = Cursors.SizeNWSE;
  113. break;
  114. case EnumMousePointPosition.TopRightCornerStretch:
  115. case EnumMousePointPosition.BottomLeftCornerStretch:
  116. Cursor.Current = Cursors.SizeNESW;
  117. break;
  118. case EnumMousePointPosition.Drag:
  119. Cursor.Current = Cursors.SizeAll;
  120. break;
  121. default:
  122. Cursor.Current = null;
  123. break;
  124. }
  125. return;
  126. }
  127. switch (mousePointPosition)
  128. {
  129. case EnumMousePointPosition.Drag:
  130. currentControl.Left += e.X - mouseDragOldPoint.X;
  131. currentControl.Top += e.Y - mouseDragOldPoint.Y;
  132. break;
  133. case EnumMousePointPosition.BottomBorderStretch:
  134. currentControl.Height += e.Y - mouseSizeLastPoint.Y;
  135. mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
  136. break;
  137. case EnumMousePointPosition.BottomRightCornerStretch:
  138. currentControl.Width += e.X - mouseSizeLastPoint.X;
  139. currentControl.Height = e.Y - mouseSizeLastPoint.Y;
  140. mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
  141. break;
  142. case EnumMousePointPosition.RightBorderStretch:
  143. currentControl.Width += e.X - mouseSizeLastPoint.X;
  144. mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
  145. break;
  146. case EnumMousePointPosition.TopBorderStretch:
  147. currentControl.Top += e.Y - mouseDragOldPoint.Y;
  148. currentControl.Height -= e.Y - mouseDragOldPoint.Y;
  149. break;
  150. case EnumMousePointPosition.LeftBorderStetch:
  151. currentControl.Left += e.X - mouseDragOldPoint.X;
  152. currentControl.Width -= e.X - mouseDragOldPoint.X;
  153. break;
  154. case EnumMousePointPosition.BottomLeftCornerStretch:
  155. currentControl.Left += e.X - mouseDragOldPoint.X;
  156. currentControl.Width -= e.X - mouseDragOldPoint.X;
  157. currentControl.Height += e.Y - mouseSizeLastPoint.Y;
  158. mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
  159. break;
  160. case EnumMousePointPosition.TopRightCornerStretch:
  161. currentControl.Top += e.Y - mouseDragOldPoint.Y;
  162. currentControl.Width += e.X - mouseSizeLastPoint.X;
  163. currentControl.Height -= e.Y - mouseDragOldPoint.Y;
  164. mouseSizeLastPoint = e.Location; //记录光标拖动的当前点
  165. break;
  166. case EnumMousePointPosition.TopLeftCornerStretch:
  167. currentControl.Left += e.X - mouseDragOldPoint.X;
  168. currentControl.Top += e.Y - mouseDragOldPoint.Y;
  169. currentControl.Width -= e.X - mouseDragOldPoint.X;
  170. currentControl.Height -= e.Y - mouseDragOldPoint.Y;
  171. break;
  172. default: break;
  173. }
  174. if (currentControl.Width < MINWIDTH)
  175. {
  176. currentControl.Width = MINWIDTH;
  177. }
  178. if (currentControl.Height < MINHEIGHT)
  179. {
  180. currentControl.Height = MINHEIGHT;
  181. }
  182. }
  183. /// <summary>
  184. /// 控件鼠标弹起事件
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. private static void DragControl_MouseUp(object sender, MouseEventArgs e)
  189. {
  190. Control currentControl = null;
  191. currentControl = sender as Control;
  192. Cursor.Current = null;
  193. mousePointPosition = EnumMousePointPosition.StateLess;
  194. currentControl.BackColor = oldColor; //恢复控件背景色
  195. }
  196. /// <summary>
  197. /// 获取鼠标位置
  198. /// </summary>
  199. /// <param name="size">控件大小</param>
  200. /// <param name="e">鼠标位置</param>
  201. /// <returns>鼠标光标位置</returns>
  202. private static EnumMousePointPosition GetMousePointPosition(Size size, MouseEventArgs e)
  203. {
  204. if ((e.X < (-1 * BAND)) || (e.X > size.Width) || (e.Y < (-1 * BAND)) || (e.Y > size.Height))//光标越界
  205. {
  206. return EnumMousePointPosition.StateLess;
  207. }
  208. if (e.X < BAND)
  209. {
  210. if (e.Y < BAND)
  211. {
  212. return EnumMousePointPosition.TopLeftCornerStretch;
  213. }
  214. else
  215. {
  216. if (e.Y > ((-1 * BAND) + size.Height))
  217. {
  218. return EnumMousePointPosition.BottomLeftCornerStretch;
  219. }
  220. else
  221. {
  222. return EnumMousePointPosition.LeftBorderStetch;
  223. }
  224. }
  225. }
  226. else
  227. {
  228. if (e.X > (-1 * BAND + size.Width))
  229. {
  230. if (e.Y < BAND)
  231. {
  232. return EnumMousePointPosition.TopRightCornerStretch;
  233. }
  234. else
  235. {
  236. if (e.Y > (-1 * BAND + size.Height))
  237. {
  238. return EnumMousePointPosition.BottomRightCornerStretch;
  239. }
  240. else
  241. {
  242. return EnumMousePointPosition.RightBorderStretch;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. if (e.Y < BAND)
  249. {
  250. return EnumMousePointPosition.TopBorderStretch;
  251. }
  252. else
  253. {
  254. if (e.Y > (-1 * BAND + size.Height))
  255. {
  256. return EnumMousePointPosition.BottomBorderStretch;
  257. }
  258. else
  259. {
  260. return EnumMousePointPosition.Drag;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. #endregion
  267. #region 绘制相关
  268. /// <summary>
  269. /// 绘制控件边框
  270. /// </summary>
  271. /// <param name="sender"></param>
  272. /// <param name="e"></param>
  273. private static void DragControlParent_Paint(object sender, PaintEventArgs e)
  274. {
  275. Control currentControl = null;
  276. currentControl = sender as Control;
  277. return;
  278. }
  279. #endregion
  280. }
  281. }