C#用鼠标在控件上绘制虚线框,并且获取矩形。

26 篇文章 0 订阅

使用

 

  1.     private SetControlRectangle Rectory;
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         { 
  4.             Rectory = new SetControlRectangle(panel1);
  5.             Rectory.SetRectangel += new SetControlRectangle.SelectRectangel(Rectory_SetRectangel);            
  6.         }
  7.         void Rectory_SetRectangel(object sneder, Rectangle e)
  8.         {
  9.             this.Text = e.ToString();
  10.         }

 

 

完整类

 

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

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值