티스토리 뷰

Drag select multiple controls



C# 마우스 드래그해서 여러개 콘트롤 선택하기 



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Drawing.Drawing2D;


namespace DragSelectMultipleControls

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            DoubleBuffered = true;

        }


        private Point selectionStart;

        private Point selectionEnd;

        private Rectangle selection;

        private bool mouseDown;


        private void GetSelectedTextBoxes()

        {


            List<TextBox> selected = new List<TextBox>();


            foreach (Control c in Controls)

            {

                if (c is TextBox)

                {

                    if (selection.IntersectsWith(c.Bounds))

                    {

                        selected.Add((TextBox)c);

                    }

                }

            }


            MessageBox.Show("you selected " + selected.Count + "textbox controls");

        }


        protected override void OnMouseDown(MouseEventArgs e)

        {

            selectionStart = PointToClient(MousePosition);

            mouseDown = true;

            base.OnMouseDown(e);

        }


        protected override void OnMouseUp(MouseEventArgs e)

        {

            mouseDown = false;

            SetSelectionRect();

            Invalidate();

            GetSelectedTextBoxes();

            base.OnMouseUp(e);

        }

        


        protected override void OnMouseMove(MouseEventArgs e)

        {

            if (!mouseDown)

                return;

            selectionEnd = PointToClient(MousePosition);

            SetSelectionRect();

            Invalidate();


            base.OnMouseMove(e);

        }

    

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint(e);

            

            if(mouseDown)

            {

                using (Pen pen = new Pen(Color.Black, 1F))

                {

                    pen.DashStyle = DashStyle.Dash;

                    e.Graphics.DrawRectangle(pen, selection);

                }

            }

        }


        private void SetSelectionRect()

        {

            int x, y;

            int width, height;


            x = selectionStart.X > selectionEnd.X ? selectionEnd.X : selectionStart.X;

            y = selectionStart.Y > selectionEnd.Y ? selectionEnd.Y : selectionStart.Y;


            width = selectionStart.X > selectionEnd.X ? selectionStart.X - selectionEnd.X : selectionEnd.X - selectionStart.X;

            height = selectionStart.Y > selectionEnd.Y ? selectionStart.Y - selectionEnd.Y : selectionEnd.Y - selectionStart.Y;


            selection = new Rectangle(x, y, width, height);

        }


    }

}



댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글