Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#
Tip/Trick

C# Selector Tool

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Jan 2013CPOL1 min read 13.5K   543   3   3
A simple selector tool in C#.

Introduction

While working on one of my latest projects, I found myself in need of a simple region selection tool. Pressed with more important functions that my app should provide, I tried searching around the web for a code, just like many times before. After a while, it was clear to me that I’ll have to do the job myself and here it is! You can take the code and implement it however you prefer.

Image 1

Using the code

It doesn’t really require a lot of code, just some creative thinking to get you on the right track. To give you a basic idea of what it’s all about, here’s a code that allows you to define a selection component into a simple form.

C#
Selection selection = null;
public Form1()
{
    InitializeComponent();
    selection = new Selection(this);
}

The selection is then implemented in a picture box class with defined methods events regarding specific mouse movements.

C#
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    selection.DrawSelection(e.Graphics);
    pictureBox1.Refresh();
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    selection.mouseDown(e);
    pictureBox1.Refresh();

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    selection.mouseUp(e);
    pictureBox1.Refresh();
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    selection.mouseMoved(e);
    pictureBox1.Refresh();
}

Of course, this is not all the work you need to do. It’s not all that simple, but it explains the logic behind solving the problem. The form above above just calls for a Selection.cs file which handles the rest. If you believe this can help you, the full code and the demo are available to the rest of the community here at CodeProject. Play around with it, use it in your apps, or just show off in front of your friends. A lot of the guys here have helped me on more than one occasion, so this is my way of saying: “Thank you!”

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionI have a tabpage .tabpage content is a form,if i want to make a form that user can resize it like visual studio; Pin
Member 1431579018-Apr-19 21:55
Member 1431579018-Apr-19 21:55 
Questionhow can i select button by this method Pin
Member 1431579018-Apr-19 21:50
Member 1431579018-Apr-19 21:50 
class Selection
    {
        public struct SelectionRecs
        {
            public Rectangle topLeft;
            public Rectangle topRight;
            public Rectangle bottomLeft;
            public Rectangle bottomRight;

            public Rectangle top;
            public Rectangle bottom;
            public Rectangle left;
            public Rectangle right;

        }
        public Selection(Form form)
        {
            this.frm = form;
        }

        public Rectangle _rec = new Rectangle();
        private Form frm = null;
        SelectionRecs selRecs = new SelectionRecs();
        //

        public Point pone = new Point();
        public Point ptwo = new Point();
        /// <summary>
        /// 
        /// </summary>
        Point ptempone = new Point();
        Point ptemptwo = new Point();

        Point startingPoint = new Point();

     Pen borderPen = new Pen(Brushes.Black, (float).00001 );

        bool mDown = false;
        bool moveAll = false;

        bool moveTL = false;
        bool moveTR = false;
        bool moveBL = false;
        bool moveBR = false;

        bool moveTop = false;
        bool moveBottom = false;
        bool moveLeft = false;
        bool moveRight = false;

        RectangleF bounds = new RectangleF();

        public Rectangle SelectionRectangle
        {
            get
            {
                return this._rec;
            }

            set
            {
                this._rec = value;
            }
        }

        public void DrawSelection(Graphics graphics)
        {
            bounds = graphics.ClipBounds;

            if (pone != Point.Empty && ptwo != Point.Empty)
            {
                Rectangle rec = new Rectangle(pone.X, pone.Y, ptwo.X - pone.X, ptwo.Y - pone.Y);
                this._rec = rec;
                graphics.DrawRectangle(borderPen, rec);

                drawSelectionShadow(graphics);
                drawSelectionRecs(graphics);
            }
            else if (mDown)
            {
                Point cpo = new Point(ptempone.X, ptempone.Y);
                Point cpt = new Point(ptemptwo.X, ptemptwo.Y);

                Rectangle rec = new Rectangle(cpo.X, cpo.Y, cpt.X - cpo.X, cpt.Y - cpo.Y);
                graphics.DrawRectangle(borderPen, rec);
            }
        }

        Point lastMouse = new Point();
        public void mouseMoved(System.Windows.Forms.MouseEventArgs mouse)
        {
            /*
            if (mouse.Location.X > bounds.Width)  
            {
               dtempone.X = (int)(bounds.Width - _rec.Width);
                ptemptwo.X = (int)bounds.Width;
            }

            if (mouse.Location.X < 0)
            {

            }

            if (mouse.Location.Y > bounds.Height || mouse.Location.Y < 0)
                return;
            */
            if (mDown)
            {
                if (mouse.Location.X < startingPoint.X && mouse.Location.Y < startingPoint.Y)
                {
                    ptempone = mouse.Location;
                    if (mouse.Location.X < 0)
                    {
                        ptempone.X = 1;
                    }

                    if (mouse.Location.Y < 0)
                    {
                        ptempone.Y = 1;
                    }
                }
                else if (mouse.Location.X < startingPoint.X && mouse.Location.Y > startingPoint.Y)
                {
                    ptempone.X = mouse.Location.X;
                    ptemptwo.Y = mouse.Location.Y;

                    if (mouse.Location.X < 0)
                    {
                        ptempone.X = 1;
                    }

                    if (mouse.Location.Y < 0)
                    {
                        ptempone.Y = 1;
                    }
                }
                else if (mouse.Location.X > startingPoint.X && mouse.Location.Y < startingPoint.Y)
                {
                    ptempone.Y = mouse.Location.Y;
                    ptemptwo.X = mouse.Location.X;

                    if (mouse.Location.X < 0)
                    {
                        ptempone.X = 1;
                    }

                    if (mouse.Location.Y < 0)
                    {
                        ptempone.Y = 1;
                    }

                }
                else
                {
                    ptemptwo = mouse.Location;

                    if (mouse.Location.X > bounds.Width)
                        ptemptwo.X = (int)bounds.Width;

                    if (mouse.Location.Y > bounds.Height)
                        ptemptwo.Y = (int)bounds.Height;

                    if (mouse.Location.X < 0)
                    {
                        ptempone.X = 1;
                    }

                    if (mouse.Location.Y < 0)
                    {
                        ptempone.Y = 1;
                    }
                }


                if (mouse.Location.X > bounds.Width)
                    ptemptwo.X = (int)bounds.Width;

                if (mouse.Location.Y > bounds.Height)
                    ptemptwo.Y = (int)bounds.Height;

                if (mouse.Location.X < 0)
                {
                    ptempone.X = 1;
                }

                if (mouse.Location.Y < 0)
                {
                    ptempone.Y = 1;
                }
            }
            else if (moveAll)
            {
                if (bounds.Contains(mouse.Location) || bounds.Contains(pone))
                {
                    Point uipone = new Point(mouse.Location.X - ptempone.X, mouse.Location.Y - ptempone.Y);
                    Point uiptwo = new Point(mouse.Location.X + ptemptwo.X, mouse.Location.Y + ptemptwo.Y);

                    if (bounds.Contains(uipone) && bounds.Contains(uiptwo))
                    {
                        pone = uipone;
                        ptwo = uiptwo;
                    }
                    else
                    {
                        if (uipone.X < 0)
                        {
                            uipone.X = 1;
                            uiptwo.X = _rec.Width + 1;
                        }

                        if (uipone.Y < 0)
                        {
                            uipone.Y = 1;
                            uiptwo.Y = _rec.Height + 1;
                        }

                        if (uiptwo.Y > (int)bounds.Height)
                        {
                            uiptwo.Y = (int)bounds.Height;
                            uipone.Y = (int)(bounds.Height - _rec.Height);
                        }

                        if (uiptwo.X > (int)bounds.Width)
                        {
                            uiptwo.X = (int)bounds.Width;
                            uipone.X = (int)(bounds.Width - _rec.Width);
                        }

                        pone = uipone;
                        ptwo = uiptwo;
                    }

                }
                else
                {
                    Point uipone = new Point(mouse.Location.X - ptempone.X, mouse.Location.Y + ptempone.Y);
                    Point uiptwo = new Point(mouse.Location.X + ptemptwo.X, mouse.Location.Y - ptemptwo.Y);

                    if (mouse.Location.Y < 0)
                    {
                        uipone.Y = 1;
                        uiptwo.Y = _rec.Height;

                    }

                    if (mouse.Location.Y > bounds.Height)
                    {
                        uipone.Y = (int)(bounds.Height - _rec.Height);
                        uiptwo.Y = (int)bounds.Height;
                    }

                    if (mouse.Location.X < 0)
                    {
                        uipone.X = 1;
                        uiptwo.X = _rec.Width;
                    }

                    if (mouse.Location.X > bounds.Width)
                    {
                        uipone.X = (int)(bounds.Width - _rec.Width);
                        uiptwo.X = (int)bounds.Width;
                    }

                    pone = uipone;
                    ptwo = uiptwo;
                }
            }
            else if (moveTL)
            {
                if (mouse.Location.X < ptwo.X - 20)
                {
                    pone.X = mouse.Location.X;
                }

                if (mouse.Location.Y < ptwo.Y - 20)
                {
                    pone.Y = mouse.Location.Y;
                }

                if (mouse.Location.X < 0)
                {
                    pone.X = 1;
                }

                if (mouse.Location.Y < 0)
                {
                    pone.Y = 1;
                }
            }
            else if (moveTR)
            {
                if (mouse.Location.X > pone.X + 20)
                {
                    ptwo.X = mouse.Location.X;
                }

                if (mouse.Location.Y < ptwo.Y - 20)
                {
                    pone.Y = mouse.Location.Y;
                }

                if (mouse.Location.Y < 0)
                {
                    pone.Y = 1;
                }

                if (mouse.Location.X > (int)bounds.Width)
                {
                    ptwo.X = (int)bounds.Width;
                }
            }
            else if (moveBL)
            {
                if (mouse.Location.X < ptwo.X - 20)
                {
                    pone.X = mouse.Location.X;
                }

                if (mouse.Location.Y > pone.Y + 20)
                {
                    ptwo.Y = mouse.Location.Y;
                }

                if (mouse.Location.Y > bounds.Height)
                {
                    ptwo.Y = (int)bounds.Height;
                }

                if (mouse.Location.X < 0)
                {
                    pone.X = 1;
                }
            }
            else if (moveBR)
            {
                if (mouse.Location.X > pone.X + 20)
                {
                    ptwo.X = mouse.Location.X;
                }

                if (mouse.Location.Y > pone.Y + 20)
                {
                    ptwo.Y = mouse.Location.Y;
                }

                if (mouse.Location.X > bounds.Width)
                {
                    ptwo.X = (int)bounds.Width;
                }

                if (mouse.Location.Y > bounds.Height)
                {
                    ptwo.Y = (int)bounds.Height;
                }
            }
            else if (moveTop)
            {
                if (mouse.Location.Y < ptwo.Y - 20)
                {
                    pone.Y = mouse.Location.Y;
                }

                if (mouse.Location.Y < 0)
                {
                    pone.Y = 1;
                }
            }
            else if (moveBottom)
            {
                if (mouse.Location.Y > pone.Y + 20)
                {
                    ptwo.Y = mouse.Location.Y;
                }

                if (mouse.Location.Y > bounds.Height)
                {
                    ptwo.Y = (int)bounds.Height;
                }
            }
            else if (moveLeft)
            {
                if (mouse.Location.X < ptwo.X - 20)
                {
                    pone.X = mouse.Location.X;
                }

                if (mouse.Location.X < 0)
                {
                    pone.X = 1;
                }
            }
            else if (moveRight)
            {
                if (mouse.Location.X > pone.X + 20)
                {
                    ptwo.X = mouse.Location.X;
                }

                if (mouse.Location.X > bounds.Width)
                {
                    ptwo.X = (int)bounds.Width;
                }
            }
            else
            {
                if (selRecs.top.Contains(mouse.Location) || selRecs.bottom.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeNS;
                }
                else if (selRecs.right.Contains(mouse.Location) || selRecs.left.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeWE;
                }
                else if (selRecs.topLeft.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeNWSE;
                }
                else if (selRecs.topRight.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeNESW;
                }
                else if (selRecs.bottomLeft.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeNESW;
                }
                else if (selRecs.bottomRight.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeNWSE;
                }
                else if (_rec.Contains(mouse.Location))
                {
                    frm.Cursor = Cursors.SizeAll;
                }
                else
                {
                    frm.Cursor = Cursors.Default;
                }

            }

            lastMouse = mouse.Location;
        }

        public void mouseDown(System.Windows.Forms.MouseEventArgs mouse)
        {
            if (frm.Cursor == Cursors.Default)
            {
                mDown = true;

                startingPoint = mouse.Location;
                pone = mouse.Location;

                ptempone = mouse.Location;
                ptemptwo = mouse.Location;

                ptwo = Point.Empty;
            }
            else
            {
                if (frm.Cursor == Cursors.SizeAll)
                {
                    moveAll = true;
                    startingPoint = mouse.Location;

                    ptempone = new Point(mouse.Location.X - pone.X, mouse.Location.Y - pone.Y);
                    ptemptwo = new Point(ptwo.X - mouse.Location.X, ptwo.Y - mouse.Location.Y);
                }
                else if (frm.Cursor == Cursors.SizeNESW)
                {
                    if (selRecs.topRight.Contains(mouse.Location))
                    {
                        moveTR = true;
                    }
                    else if (selRecs.bottomLeft.Contains(mouse.Location))
                    {
                        moveBL = true;
                    }
                }
                else if (frm.Cursor == Cursors.SizeNWSE)
                {
                    if (selRecs.topLeft.Contains(mouse.Location))
                    {
                        moveTL = true;
                    }
                    else if (selRecs.bottomRight.Contains(mouse.Location))
                    {
                        moveBR = true;
                    }
                }
                else if (frm.Cursor == Cursors.SizeWE)
                {
                    if (selRecs.left.Contains(mouse.Location))
                    {
                        moveLeft = true;
                    }
                    else
                    {
                        moveRight = true;
                    }
                }
                else if (frm.Cursor == Cursors.SizeNS)
                {
                    if (selRecs.top.Contains(mouse.Location))
                    {
                        moveTop = true;
                    }
                    else
                    {
                        moveBottom = true;
                    }
                }
            }
        }

        public void mouseUp(System.Windows.Forms.MouseEventArgs mouse)
        {
            if (mDown)
            {
                mDown = false;
                pone = ptempone;
                ptwo = ptemptwo;

                if (ptwo.X - pone.X < 20 && ptwo.Y - pone.Y < 20)
                {
                    pone = Point.Empty;
                    ptwo = Point.Empty;
                    _rec = Rectangle.Empty;
                    ptempone = Point.Empty;
                    ptemptwo = Point.Empty;
                    selRecs.topLeft = selRecs.top = selRecs.topRight = selRecs.left = selRecs.bottomLeft = selRecs.bottom = selRecs.bottomRight = selRecs.right = Rectangle.Empty;
                }

                ptemptwo = Point.Empty;
                ptempone = Point.Empty;
            }
            else
            {
                if (moveAll)
                {
                    moveAll = false;
                }
                else if (moveTL)
                {
                    moveTL = false;
                }
                else if (moveTR)
                {
                    moveTR = false;
                }
                else if (moveBL)
                {
                    moveBL = false;
                }
                else if (moveBR)
                {
                    moveBR = false;
                }
                else if (moveTop)
                {
                    moveTop = false;
                }
                else if (moveBottom)
                {
                    moveBottom = false;
                }
                else if (moveLeft)
                {
                    moveLeft = false;
                }
                else if (moveRight)
                {
                    moveRight = false;
                }


            }
        }

        public void correctPoints(Point one, Point two)
        {
            if (one.X > two.X)
            {
                int tmpx = one.X;
                one.X = two.X;
                two.X = tmpx;
            }

            if (one.Y > two.Y)
            {
                int tmpy = one.Y;
                one.Y = two.Y;
                two.Y = tmpy;
            }
        }

        public void drawSelectionRecs(Graphics graph)
        {
            selRecs.topLeft = new Rectangle(pone.X - 4, pone.Y - 4, 8, 8);
            selRecs.topRight = new Rectangle(ptwo.X - 4, pone.Y - 4, 8, 8);
            selRecs.bottomLeft = new Rectangle(pone.X - 4, ptwo.Y - 4, 8, 8);
            selRecs.bottomRight = new Rectangle(ptwo.X - 4, ptwo.Y - 4, 8, 8);

            selRecs.top = new Rectangle(((ptwo.X - pone.X) / 2) + pone.X - 3, pone.Y - 3, 6, 6);
            selRecs.bottom = new Rectangle(((ptwo.X - pone.X) / 2) + pone.X - 3, ptwo.Y - 3, 6, 6);
            selRecs.left = new Rectangle(pone.X - 3, (((ptwo.Y - pone.Y) / 2) + pone.Y) - 3, 6, 6);
            selRecs.right = new Rectangle(ptwo.X - 3, (((ptwo.Y - pone.Y) / 2) + pone.Y) - 3, 6, 6);

            graph.DrawRectangle(borderPen, selRecs.topLeft);
            graph.DrawRectangle(borderPen, selRecs.topRight);

            graph.DrawRectangle(borderPen, selRecs.bottomLeft);
            graph.DrawRectangle(borderPen, selRecs.bottomRight);

            graph.DrawRectangle(borderPen, selRecs.top);
            graph.DrawRectangle(borderPen, selRecs.bottom);
            graph.DrawRectangle(borderPen, selRecs.left);
            graph.DrawRectangle(borderPen, selRecs.right);
        }

        public void drawSelectionShadow(Graphics graph)
        {
            Color transColor = Color.FromArgb(32, 0, 0, 0);
            Brush brsh = new SolidBrush(transColor);

            graph.FillRectangle(brsh, new Rectangle(pone.X, 0, (int)graph.ClipBounds.Width * 2, pone.Y));
            graph.FillRectangle(brsh, new Rectangle(0, 0, pone.X, (int)graph.ClipBounds.Height));

            graph.FillRectangle(brsh, new Rectangle(ptwo.X, pone.Y, (int)graph.ClipBounds.Width - ptwo.X, (int)graph.ClipBounds.Height - pone.Y));
            graph.FillRectangle(brsh, new Rectangle(pone.X, ptwo.Y, ptwo.X - pone.X, (int)graph.ClipBounds.Height - ptwo.Y));
        }

Praisethanx Pin
Member 120369289-Nov-15 23:36
Member 120369289-Nov-15 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.