c# 模拟windows桌面鼠标拖选 张宇轩

来源:互联网 发布:鸿鹄安知燕雀之志王侯 编辑:程序博客网 时间:2024/04/29 23:24

命名空间
using System.Drawing;

定义两个变量
bool MouseIsDown=false;
Rectangle MouseRect  
=   Rectangle.Empty;

定义三个方法
private void ResizeToRectangle(Point   p)
{
    DrawRectangle();
    MouseRect.Width
= p.X - MouseRect.Left;
    MouseRect.Height
= p.Y - MouseRect.Top;
    DrawRectangle();
}
private void DrawRectangle()
{
   Rectangle rect
= this.RectangleToScreen(MouseRect);
   ControlPaint.DrawReversibleFrame(rect,Color.White,FrameStyle.Dashed);
}

private void DrawStart(Point StartPoint)
{
  
this.Capture = true;
   Cursor.Clip
= this.RectangleToScreen(this.Bounds);
   MouseRect
= new Rectangle(StartPoint.X,StartPoint.Y,0,0);
}

在鼠标按下事件里写(一定是鼠标按下事件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;

原创粉丝点击