现在的位置: 首页 > 综合 > 正文

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

2012年08月03日 ⁄ 综合 ⁄ 共 4200字 ⁄ 字号 评论关闭

 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)= 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)
    {
    }
  }

{
MouseIsDown

 

 

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

抱歉!评论已关闭.