网上找来点代码.....能够拉出虚线框了........但是不知道怎么选择控件...Form窗体上...this.GetChildAtPoint(e.Location); 用这个方法 但是不知道放什么地方(放那个事件里触发好);或者谁有其他的方法........思路说说.......自己琢磨了变天搞出不来哈
代码在下面...  public partial class Form1 : Form
  {
    bool MouseIsDown = false;
    Rectangle MouseRect = Rectangle.Empty;
    public Form1()
    {
      InitializeComponent();
      this.MouseDown += new MouseEventHandler(Form1_MouseDown);
      this.MouseMove += new MouseEventHandler(Form1_MouseMove);
      this.MouseUp += new MouseEventHandler(Form1_MouseUp);
    }    void Form1_MouseDown(object sender, MouseEventArgs e)
    {
      MouseIsDown = true;
      DrawStart(e.Location);
    }    void Form1_MouseMove(object sender, MouseEventArgs e)
    {
      if (MouseIsDown)
      {
        ResizeToRectangle(e.Location); 
      }
      //这里可以实现选中啊,但是必须要鼠标在子控件上活动过(鼠标路过子控件)才能选中
      //Control ctl = this.GetChildAtPoint(e.Location);
      //if (ctl != null)
      //{
      //  ctl.BackColor = Color.Blue;
      //}
    }    void Form1_MouseUp(object sender, MouseEventArgs e)
    {
      this.Capture = false;
      Cursor.Clip = Rectangle.Empty;
      MouseIsDown = false;
      DrawRectangle();
      MouseRect = Rectangle.Empty;
    }    private void Form1_Load(object sender, EventArgs e)
    {    }    private void ResizeToRectangle(Point p)
    {
      DrawRectangle();
      MouseRect.Width = p.X - MouseRect.Left;
      MouseRect.Height = p.Y - MouseRect.Top;
      DrawRectangle();
    }    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);
    }    private void DrawRectangle()
    {
      Rectangle rect = this.RectangleToScreen(MouseRect);
      ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
    }    private void Form1_ControlAdded(object sender, ControlEventArgs e)
    {
    }
  }

解决方案 »

  1.   

    这个问题奇怪吗,不是吧,还是我米说清楚........就像Windows桌面,点击鼠标左键一拖.........桌面上的文件或者快捷方式就选中我想只要是容器 都应该可以实现这个功能的.........
      

  2.   

    貌似有点困难 
     ControlPaint.DrawReversibleFrame 方法 先绘制  鼠标移动一下 就把老到的rectangle删掉 直到鼠标左键弹起 获得最终的矩形 
    然后通过遍历控件坐标 用Rectangle.Contain(Point)方法 判断是否包括
      

  3.   

    笨方法,遍历窗体的力所有控件的location,如果在虚线的范围内就选中
    不过还要考虑控件本身的长和高
      

  4.   

    Rectangle.Intersect就可以满足你的要求了
      

  5.   

    定义个全局变量 int flag=-1;
    如果画虚线的方法执行了 flag=0;
    在鼠标弹起的事件里
    {
       if(flag==0)
       {
          //判断控件
           flag=-1;
       }
    }
      

  6.   

    mousedown mousemove mouseup都要用
      

  7.   

    Rectangle.Intersect 咋满足啊......???Rectangle.Intersect好像要得到两个矩形框......子控件的Rectangle咋得到呢??
      

  8.   

    子控件的Rectangle可以事先遍历得到
      

  9.   

    using System.Windows.Forms;
    using System.Drawing;
    namespace TestApp
    {
        partial class Form2
        {        private System.ComponentModel.IContainer components = null;        protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        private void InitializeComponent()
            {
                this.cb1 = new CheckBox();
                this.cb3 = new CheckBox();
                this.cb5 = new CheckBox();
                this.cb6 = new CheckBox();
                this.cb7 = new CheckBox();
                this.cb8 = new CheckBox();
                this.cb9 = new CheckBox();
                this.cb10 = new CheckBox();
                this.cb2 = new CheckBox();
                this.cb4 = new CheckBox();
                this.cb11 = new CheckBox();
                this.cb12 = new CheckBox();
                this.cb13 = new CheckBox();
                this.cb14 = new CheckBox();
                this.cb15 = new CheckBox();
                this.cb16 = new CheckBox();
                this.cb17 = new CheckBox();
                this.cb18 = new CheckBox();            this.SuspendLayout();
                // 
                // cb1
                // 
                this.cb1.AutoSize = true;
                this.cb1.BackgroundImageLayout = ImageLayout.None;
                this.cb1.Location = new Point(29, 51);
                this.cb1.Name = "cb1";
                this.cb1.Size = new Size(80, 17);
                this.cb1.TabIndex = 0;
                this.cb1.Text = "cb1";
                this.cb1.UseVisualStyleBackColor = true;
                // 
                // cb3
                // 
                this.cb3.AutoSize = true;
                this.cb3.Location = new Point(29, 75);
                this.cb3.Name = "cb3";
                this.cb3.Size = new Size(80, 17);
                this.cb3.TabIndex = 2;
                this.cb3.Text = "cb3";
                this.cb3.UseVisualStyleBackColor = true;
                // 
                // cb5
                // 
                this.cb5.AutoSize = true;
                this.cb5.Location = new Point(29, 98);
                this.cb5.Name = "cb5";
                this.cb5.Size = new Size(80, 17);
                this.cb5.TabIndex = 4;
                this.cb5.Text = "cb5";
                this.cb5.UseVisualStyleBackColor = true;
                // 
                // cb6
                // 
                this.cb6.AutoSize = true;
                this.cb6.Location = new Point(29, 121);
                this.cb6.Name = "cb6";
                this.cb6.Size = new Size(80, 17);
                this.cb6.TabIndex = 5;
                this.cb6.Text = "cb6";
                this.cb6.UseVisualStyleBackColor = true;
                // 
                // cb7
                // 
                this.cb7.AutoSize = true;
                this.cb7.Location = new Point(29, 212);
                this.cb7.Name = "cb7";
                this.cb7.Size = new Size(80, 17);
                this.cb7.TabIndex = 9;
                this.cb7.Text = "cb7";            // 
                // cb8
                // 
                this.cb8.AutoSize = true;
                this.cb8.Location = new Point(29, 189);
                this.cb8.Name = "cb8";            this.cb8.TabIndex = 8;
                this.cb8.Text = "cb8";
                this.cb8.UseVisualStyleBackColor = true;
                // 
                // cb9
                // 
                this.cb9.AutoSize = true;
                this.cb9.Location = new Point(29, 166);
                this.cb9.Name = "cb9";
                this.cb9.Size = new Size(80, 17);
                this.cb9.TabIndex = 7;
                this.cb9.Text = "cb9";
                this.cb9.UseVisualStyleBackColor = true;
                // 
                // cb10
                // 
                this.cb10.AutoSize = true;
                this.cb10.Location = new Point(29, 142);
                this.cb10.Name = "cb10";
                this.cb10.Size = new Size(86, 17);
                this.cb10.TabIndex = 6;
                this.cb10.Text = "cb10";
                this.cb10.UseVisualStyleBackColor = true;
                // 
                // cb2
                // 
                this.cb2.AutoSize = true;
                this.cb2.Location = new Point(115, 212);
                this.cb2.Name = "cb2";
                this.cb2.Size = new Size(80, 17);
                this.cb2.TabIndex = 17;
                this.cb2.Text = "cb2";
                this.cb2.UseVisualStyleBackColor = true;
                // 
                // cb4
                // 
                this.cb4.AutoSize = true;
                this.cb4.Location = new Point(115, 189);
                this.cb4.Name = "cb4";
                this.cb4.Size = new Size(80, 17);
                this.cb4.TabIndex = 16;
                this.cb4.Text = "cb4";
                this.cb4.UseVisualStyleBackColor = true;
                // 
                // cb11
                // 
                this.cb11.AutoSize = true;
                this.cb11.Location = new Point(115, 166);
                this.cb11.Name = "cb11";
                this.cb11.Size = new Size(86, 17);
                this.cb11.TabIndex = 15;
                this.cb11.Text = "cb11";
                this.cb11.UseVisualStyleBackColor = true;
                // 
                // cb12
                // 
                this.cb12.AutoSize = true;
                this.cb12.Location = new Point(115, 142);
                this.cb12.Name = "cb12";
                this.cb12.Size = new Size(86, 17);
                this.cb12.TabIndex = 14;
                this.cb12.Text = "cb12";
                this.cb12.UseVisualStyleBackColor = true;
                // 
                // cb13
                // 
                this.cb13.AutoSize = true;
                this.cb13.Location = new Point(115, 121);
                this.cb13.Name = "cb13";
                this.cb13.Size = new Size(86, 17);
                this.cb13.TabIndex = 13;
                this.cb13.Text = "cb13";
                this.cb13.UseVisualStyleBackColor = true;
                // 
                // cb14
                // 
                this.cb14.AutoSize = true;
                this.cb14.Location = new Point(115, 98);
                this.cb14.Name = "cb14";
                this.cb14.Size = new Size(86, 17);
                this.cb14.TabIndex = 12;
                this.cb14.Text = "cb14";
                this.cb14.UseVisualStyleBackColor = true;
                // 
                // cb15
                // 
                this.cb15.AutoSize = true;
                this.cb15.Location = new Point(115, 75);
                this.cb15.Name = "cb15";
                this.cb15.Size = new Size(86, 17);
                this.cb15.TabIndex = 11;
                this.cb15.Text = "cb15";
                this.cb15.UseVisualStyleBackColor = true;
                // 
                // cb16
                // 
                this.cb16.AutoSize = true;
                this.cb16.Location = new Point(115, 51);
                this.cb16.Name = "cb16";
                this.cb16.Size = new Size(86, 17);
                this.cb16.TabIndex = 10;
                this.cb16.Text = "cb16";
                this.cb16.UseVisualStyleBackColor = true;
                // 
                // cb17
                // 
                this.cb17.AutoSize = true;
                this.cb17.Location = new Point(220, 212);
                this.cb17.Name = "cb17";
                this.cb17.Size = new Size(86, 17);
                this.cb17.TabIndex = 25;
                this.cb17.Text = "cb17";
                this.cb17.UseVisualStyleBackColor = true;
      
                // 
                // Form2
                // 
                this.AllowDrop = true;
                this.AutoScaleDimensions = new SizeF(6F, 13F);
                this.AutoScaleMode = AutoScaleMode.Font;
                this.ClientSize = new Size(403, 297);
                this.Controls.Add(this.cb17);
                this.Controls.Add(this.cb2);
                this.Controls.Add(this.cb4);
                this.Controls.Add(this.cb11);
                this.Controls.Add(this.cb12);
                this.Controls.Add(this.cb13);
                this.Controls.Add(this.cb14);
                this.Controls.Add(this.cb15);
                this.Controls.Add(this.cb16);
                this.Controls.Add(this.cb7);
                this.Controls.Add(this.cb8);
                this.Controls.Add(this.cb9);
                this.Controls.Add(this.cb10);
                this.Controls.Add(this.cb6);
                this.Controls.Add(this.cb5);
                this.Controls.Add(this.cb3);
                this.Controls.Add(this.cb1);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private CheckBox cb1;
            private CheckBox cb3;
            private CheckBox cb5;
            private CheckBox cb6;
            private CheckBox cb7;
            private CheckBox cb8;
            private CheckBox cb9;
            private CheckBox cb10;
            private CheckBox cb2;
            private CheckBox cb4;
            private CheckBox cb11;
            private CheckBox cb12;
            private CheckBox cb13;
            private CheckBox cb14;
            private CheckBox cb15;
            private CheckBox cb16;
            private CheckBox cb17;
            private CheckBox cb18;
        }
    }
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace TestApp
    {
        public partial class Form2 : Form
        {
            bool MouseIsDown = false;
            Rectangle MouseRect = Rectangle.Empty;
            public Form2()
            {
                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();            foreach (Control ct in this.Controls)
                {
                    // Point ctrlCenter = new Point(ct.Location.X + ct.Width / 2, ct.Location.Y + ct.Height / 2);                //if (ctrlCenter.X > MouseRect.Location.X
                    //    && ctrlCenter.X  < MouseRect.Location.X + MouseRect.Width
                    //    && ctrlCenter.Y > MouseRect.Location.Y && ctrlCenter.Y > MouseRect.Location.Y + MouseRect.Height)
                    if (MouseRect.Contains(ct.Location))
                    {
                        if (ct is CheckBox)
                        {
                            if (((CheckBox)ct).Checked) //值选中
                            {
                                ((CheckBox)ct).Checked = false;
                            }
                            else
                            {
                                ((CheckBox)ct).Checked = true;
                            }
                            //控件选中 
                           
                        }
                    }
                }            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);
            }
        }
    }
      

  11.   

    测试了一下楼上的  用VS2008 提示两个问题
    1、this.AutoScaleMode = AutoScaleMode.Font;提示不能手动更改
    2、using System.Linq;  这个08里面的吧
      

  12.   

    其实就 这些代码有用,其它都是测试用的 代码太长了 提交不了 所以才精简了
    MouseRect.Contains(ct.Location)这个算法是可以扩展的          foreach (Control ct in this.Controls)
                {
                    // Point ctrlCenter = new Point(ct.Location.X + ct.Width / 2, ct.Location.Y + ct.Height / 2);                //if (ctrlCenter.X > MouseRect.Location.X
                    //    && ctrlCenter.X  < MouseRect.Location.X + MouseRect.Width
                    //    && ctrlCenter.Y > MouseRect.Location.Y && ctrlCenter.Y > MouseRect.Location.Y + MouseRect.Height)
                    if (MouseRect.Contains(ct.Location))
                    {
                        if (ct is CheckBox)
                        {
      

  13.   

    Rectangle.Intersect()  还是不行了, 不知道在哪个事件里触发好......不用到循环最好了
      

  14.   


    最后获取选中的控件 当然是在mouseup里了
    用rectangle的Contain(Point)啊
    point就是控件的location
      

  15.   

    1)把最后画的矩形保存下来,rect,
    2)在MouseUp 事件里,建立画笔,
    判断控件的位置是否在rect范围之内.如果在的话给其自绘选中效果,我这做了,
    可以实现,但是刷新时有点小问题,没空调了,你自己试下吧Graphics g = Graphics.FromHwnd(this.Handle);
    //在此判断控件的位置,如果在选中范围内,刚给其自绘选中效果
    {
        g.DrawRectangle(new Pen(Brushes.Red, 1), 
                            new Rectangle(ctl.Location.X - 1, ctl.Location.Y - 1, 
                            ctl.Size.Width + 1, ctl.Size.Height + 1));
                        g.Dispose();
    }
      

  16.   

    小改一下,呵呵foreach(Control ctl in this.Controls){
        Graphics g = Graphics.FromHwnd(this.Handle);
        //在此判断控件的位置,如果在选中范围内,则给其自绘选中效果
       if(控件在选中矩形范围内)
        {
            g.DrawRectangle(new Pen(Brushes.Red, 1), 
                            new Rectangle(ctl.Location.X - 1, ctl.Location.Y - 1, 
                            ctl.Size.Width + 1, ctl.Size.Height + 1));
                        g.Dispose();
        }
    }
      

  17.   

    谢谢 各位了.......大家都是写在MouseUp事件里的.......谁能弄个MouseMove 事件里写个了 我觉得还是要用这个方法:  this.GetChildAtPoint(坐标);  现在就是不知道 虚线和子控件 交叉的一瞬间 怎么去触发一个事件,得到一个(坐标)......我相信应该是可以实现的
      

  18.   

    用 zcl24 的方法可以啊。只要在MouseUp是修正下 MouseRect 的值就可以了
    void frmMain_MouseUp(object sender, MouseEventArgs e)
            {
                this.Capture = false;
                Cursor.Clip = Rectangle.Empty;
                MouseIsDown = false;
                DrawRectangle();
                MouseRect = rectCorrection(MouseRect);
                foreach (Control ct in this.Controls)
                {
                    // Point ctrlCenter = new Point(ct.Location.X + ct.Width / 2, ct.Location.Y + ct.Height / 2);                //if (ctrlCenter.X > MouseRect.Location.X
                    //    && ctrlCenter.X  < MouseRect.Location.X + MouseRect.Width
                    //    && ctrlCenter.Y > MouseRect.Location.Y && ctrlCenter.Y > MouseRect.Location.Y + MouseRect.Height)
                    if (MouseRect.Contains(ct.Location))
                    {
                        if (ct is CheckBox)
                        {
                            if (((CheckBox)ct).Checked) //值选中
                            {
                                ((CheckBox)ct).Checked = false;
                            }
                            else
                            {
                                ((CheckBox)ct).Checked = true;
                            }
                            //控件选中                     }
                    }
                }            MouseRect = Rectangle.Empty;
            }
            //修正MouseRect 的值
            Rectangle rectCorrection(Rectangle oldRect)
            {
                if (oldRect.Width < 0)
                {
                    oldRect.X += oldRect.Width;
                    oldRect.Width *= -1;
                }
                if (oldRect.Height < 0)
                {
                    oldRect.Y += oldRect.Height;
                    oldRect.Height *= -1;
                }
                return new Rectangle(oldRect.X, oldRect.Y, oldRect.Width, oldRect.Height);
            }