SetControlRectangle.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace DllXqManager
  9. {
  10. /// <summary>
  11. /// 在一个控件上绘制虚线矩形 并返回矩形区域
  12. /// zgke@sina.com.
  13. /// qq:116149
  14. /// </summary>
  15. public class SetControlRectangle
  16. {
  17. /// <summary>
  18. /// 需要绘制的图形
  19. /// </summary>
  20. /// <param name="p_Control">控件</param>
  21. public SetControlRectangle(Control p_Control)
  22. {
  23. m_Control = p_Control;
  24. m_Control.MouseUp += new MouseEventHandler(MyControl_MouseUp);
  25. m_Control.MouseMove += new MouseEventHandler(MyControl_MouseMove);
  26. m_Control.MouseDown += new MouseEventHandler(MyControl_MouseDown);
  27. }
  28. private Control m_Control;
  29. /// <summary>
  30. /// 鼠标状态
  31. /// </summary>
  32. private bool m_MouseIsDown = false;
  33. /// <summary>
  34. /// 绘制区域
  35. /// </summary>
  36. private Rectangle m_MouseRect = Rectangle.Empty;
  37. /// <summary>
  38. /// 刷新绘制
  39. /// </summary>
  40. /// <param name="p"></param>
  41. private void ResizeToRectangle(Point p_Point)
  42. {
  43. DrawRectangle();
  44. m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
  45. m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
  46. DrawRectangle();
  47. }
  48. /// <summary>
  49. /// 绘制区域
  50. /// </summary>
  51. private void DrawRectangle()
  52. {
  53. Rectangle _Rect = m_Control.RectangleToScreen(m_MouseRect);
  54. ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
  55. }
  56. /// <summary>
  57. /// 开始绘制 并且设置鼠标区域
  58. /// </summary>
  59. /// <param name="StartPoint">开始位置</param>
  60. private void DrawStart(Point p_Point)
  61. {
  62. m_Control.Capture = true;
  63. Cursor.Clip = m_Control.RectangleToScreen(new Rectangle(0, 0, m_Control.Width, m_Control.Height));
  64. m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
  65. }
  66. private void MyControl_MouseDown(object sender, MouseEventArgs e)
  67. {
  68. m_MouseIsDown = true;
  69. DrawStart(new Point(e.X, e.Y));
  70. //m_Control.Refresh();//刷新容器,清空容器上的绘画
  71. foreach (var item in MoveControl.CurrList)
  72. item.IsSelected = false;
  73. MoveControl.ClearFrameControl();
  74. }
  75. private void MyControl_MouseMove(object sender, MouseEventArgs e)
  76. {
  77. if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
  78. }
  79. private void MyControl_MouseUp(object sender, MouseEventArgs e)
  80. {
  81. m_Control.Capture = false;
  82. Cursor.Clip = Rectangle.Empty;
  83. m_MouseIsDown = false;
  84. DrawRectangle();
  85. if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
  86. {
  87. //如果区域没0 就不执行委托
  88. }
  89. else
  90. {
  91. if (SetRectangel != null) SetRectangel(m_Control, m_MouseRect);
  92. }
  93. m_MouseRect = Rectangle.Empty;
  94. }
  95. public delegate void SelectRectangel(object sneder, Rectangle e);
  96. public event SelectRectangel SetRectangel;
  97. }
  98. }