rt,谢谢!

解决方案 »

  1.   

    using   System; 
    using   System.Collections.Generic; 
    using   System.ComponentModel; 
    using   System.Data; 
    using   System.Drawing; 
    using   System.Text; 
    using   System.Windows.Forms; namespace   Probation 

            public   partial   class   frmMain   :   Form 
            { 
                    bool   MouseIsDown   =   false; 
                    Rectangle   MouseRect   =   Rectangle.Empty;   
                    public   frmMain() 
                    { 
                            InitializeComponent(); 
                            this.MouseDown+=new   MouseEventHandler(frmMain_MouseDown); 
                            this.MouseMove+=new   MouseEventHandler(frmMain_MouseMove); 
                            this.MouseUp   +=new   MouseEventHandler(frmMain_MouseUp); 
                    } 
                    void     frmMain_MouseUp(object   sender,   MouseEventArgs   e) 
                    { 
                            this.Capture   =   false;   
                            Cursor.Clip   =   Rectangle.Empty;   
                            MouseIsDown   =   false;   
                            DrawRectangle(); 
                            MouseRect   =   Rectangle.Empty; 
                    } 
                    void     frmMain_MouseMove(object   sender,   MouseEventArgs   e) 
                    { 
                            if   (MouseIsDown)   
                                  ResizeToRectangle(e.Location);   
                    } 
                    void     frmMain_MouseDown(object   sender,   MouseEventArgs   e) 
                    { 
                            MouseIsDown   =   true; 
                            DrawStart(e.Location);   
                    } 
                    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(new   Rectangle(0,   0,   ClientSize.Width,   ClientSize.Height)); 
                            MouseRect   =   new   Rectangle(StartPoint.X,   StartPoint.Y,   0,   0);   
                    }   
            } 
    }
      

  2.   

    命名空间 
    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;
      

  3.   

    Cursor.Clip = this.RectangleToScreen(this.Bounds); 
    的作用是限制鼠标不要超出绘图窗口,RectangleToScreen是转换到屏幕坐标系。这里的this与实际的绘图部分不一致时就有问题了,因此需要根据情况选择。
      

  4.   

    我试了一下,可是我拉出的虚线框并没有消失,应该是OnMouseUp事件出问题了,可是就是解决不了,郁闷:-(