c# winform 用鼠标画拉出来的虚线框,鼠标框选边框效果

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace AllTest  
  10. {  
  11.     public partial class Form11 : Form  
  12.     {  
  13.         bool MouseIsDown = false;  
  14.         Rectangle MouseRect = Rectangle.Empty;   
  15.         public Form11()  
  16.         {  
  17.             InitializeComponent();  
  18.             this.MouseDown += new MouseEventHandler(frmMain_MouseDown);  
  19.             this.MouseMove += new MouseEventHandler(frmMain_MouseMove);  
  20.             this.MouseUp += new MouseEventHandler(frmMain_MouseUp);   
  21.         }  
  22.         void frmMain_MouseUp(object sender, MouseEventArgs e)  
  23.         {  
  24.             this.Capture = false;  
  25.             Cursor.Clip = Rectangle.Empty;  
  26.             MouseIsDown = false;  
  27.             DrawRectangle();  
  28.             MouseRect = Rectangle.Empty;  
  29.         }  
  30.         void frmMain_MouseMove(object sender, MouseEventArgs e)  
  31.         {  
  32.             if (MouseIsDown)  
  33.                 ResizeToRectangle(e.Location);  
  34.         }  
  35.         void frmMain_MouseDown(object sender, MouseEventArgs e)  
  36.         {  
  37.             MouseIsDown = true;  
  38.             DrawStart(e.Location);  
  39.         }  
  40.         private void ResizeToRectangle(Point p)  
  41.         {  
  42.             DrawRectangle();  
  43.             MouseRect.Width = p.X - MouseRect.Left;  
  44.             MouseRect.Height = p.Y - MouseRect.Top;  
  45.             DrawRectangle();  
  46.         }  
  47.         private void DrawRectangle()  
  48.         {  
  49.             Rectangle rect = this.RectangleToScreen(MouseRect);  
  50.             ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);  
  51.         }  
  52.         private void DrawStart(Point StartPoint)  
  53.         {  
  54.             this.Capture = true;  
  55.             Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));  
  56.             MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);  
  57.         }   
  58.     }  
  59. }  
在鼠标按下事件里写(一定是鼠标按下事件MouseDown 因为我的参数e是鼠标数据对象 
(不过你也可以传坐标)) 
MouseIsDown = true; 
DrawStart(e.Location); 

在鼠标移动(MouseMove)事件里写 
if (MouseIsDown) 
  ResizeToRectangle(e.Location); 


在鼠标释放(MouseUp)事件里写 
this.Capture = false; 
Cursor.Clip = Rectangle.Empty; 
MouseIsDown = false; 
DrawRectangle(); 
MouseRect = Rectangle.Empty; 

这样不会覆盖原来的控件,不会重绘控件 

如果是加Panel的话 

把DrawStart()方法里的Cursor.Clip = this.RectangleToScreen(this.Bounds); 
改为Cursor.Clip = this.RectangleToScreen(this.panel1.ClientRectangle)); 
this.Capture = true;改为 this.panel1.Capture = true; 
MouseUp里的 this.Capture = false;改为 
this.panel1.Capture = false; 

这是设置鼠标筐选时鼠标的移动区域 和控件对鼠标的捕获 
Cursor.Clip = this.RectangleToScreen(this.Bounds); 
这是设置鼠标筐选时鼠标的移动区域 根据你的需要自己去设置 

黑色头发:http://heisetoufa.iteye.com/
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值