using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DllXqManager { /// /// 在一个控件上绘制虚线矩形 并返回矩形区域 /// zgke@sina.com. /// qq:116149 /// public class SetControlRectangle { /// /// 需要绘制的图形 /// /// 控件 public SetControlRectangle(Control p_Control) { m_Control = p_Control; m_Control.MouseUp += new MouseEventHandler(MyControl_MouseUp); m_Control.MouseMove += new MouseEventHandler(MyControl_MouseMove); m_Control.MouseDown += new MouseEventHandler(MyControl_MouseDown); } private Control m_Control; /// /// 鼠标状态 /// private bool m_MouseIsDown = false; /// /// 绘制区域 /// private Rectangle m_MouseRect = Rectangle.Empty; /// /// 刷新绘制 /// /// private void ResizeToRectangle(Point p_Point) { DrawRectangle(); m_MouseRect.Width = p_Point.X - m_MouseRect.Left; m_MouseRect.Height = p_Point.Y - m_MouseRect.Top; DrawRectangle(); } /// /// 绘制区域 /// private void DrawRectangle() { Rectangle _Rect = m_Control.RectangleToScreen(m_MouseRect); ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed); } /// /// 开始绘制 并且设置鼠标区域 /// /// 开始位置 private void DrawStart(Point p_Point) { m_Control.Capture = true; Cursor.Clip = m_Control.RectangleToScreen(new Rectangle(0, 0, m_Control.Width, m_Control.Height)); m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0); } private void MyControl_MouseDown(object sender, MouseEventArgs e) { m_MouseIsDown = true; DrawStart(new Point(e.X, e.Y)); //m_Control.Refresh();//刷新容器,清空容器上的绘画 foreach (var item in MoveControl.CurrList) item.IsSelected = false; MoveControl.ClearFrameControl(); } private void MyControl_MouseMove(object sender, MouseEventArgs e) { if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y)); } private void MyControl_MouseUp(object sender, MouseEventArgs e) { m_Control.Capture = false; Cursor.Clip = Rectangle.Empty; m_MouseIsDown = false; DrawRectangle(); if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0) { //如果区域没0 就不执行委托 } else { if (SetRectangel != null) SetRectangel(m_Control, m_MouseRect); } m_MouseRect = Rectangle.Empty; } public delegate void SelectRectangel(object sneder, Rectangle e); public event SelectRectangel SetRectangel; } }