C# Winform 窗体用鼠标拖出虚线框....并且虚线框区域里的所有控件选中

c#需要框选多个控件的一般方法

新建form2 代码如下

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 Win
{
    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);

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

 

       
        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);
        }

        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);
        }
    }
}
 

Form2.Designer.cs界面布局如下

using System.Drawing;
using System.Windows.Forms;

namespace Win
{
    partial class Form2
    {
      
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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();
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);


            // 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();

        }
        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;
        #endregion
    }
}

 

完成以后代码界面如下:

可以鼠标拖动选择相应代码;

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值