C# Winform 表單用滑鼠拖出虛線框….並且虛線框地區裡的所有控制項選中

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.