Home All Groups Group Topic Archive Search About

How to draw a focus rect?

Author
14 Aug 2005 7:45 AM
Johnny H
Using: ms framework 1.1

How to mark a selected region in a picturebox control?


Thanks IA

JH

Author
14 Aug 2005 12:34 PM
Mark R. Dawson
Hi Johnny,
  if you want to draw a rectangle like a selection region in your picturebox
you could create your own picturebox control that inherits from the standard
PictureBox and then override the Paint method to add drawing a rectangle,
like:


class MyPictureBox : PictureBox
    {
        /// <summary>
        /// Indicates if the mouse button is currently pressed
        /// </summary>
        private bool m_blnMouseDown = false;

        /// <summary>
        /// The rectangle indicating where to draw the selection rectangle
        /// </summary>
        private Rectangle m_rectSelection;

        /// <summary>
        /// The point where the mouse was pressed down
        /// </summary>
        private Point m_pntMouseDown;


        public MyPictureBox() : base()
        {
            //Add event handlers for mouse events
            this.MouseDown += new MouseEventHandler(MyPictureBox_MouseDown);
            this.MouseUp += new MouseEventHandler(MyPictureBox_MouseUp);
            this.MouseMove += new MouseEventHandler(MyPictureBox_MouseMove);
        }

        private void MyPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            //store the point at which the mouse was pressed
            m_pntMouseDown = new Point(e.X, e.Y);

            //indicate that the mouse button is down
            m_blnMouseDown = true;
        }

        private void MyPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            m_blnMouseDown = false;

            //redraw the image to remove the reactangle, now that
            //the mouse has been released
            this.Invalidate();
        }

        private void MyPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            Point pSource, pMouse;
            Size s;

            if(m_blnMouseDown)
            {
                //current mouse point
                pMouse= new Point(e.X, e.Y);

                //the minimum point (need to take into account negative number situations
                pSource = this.GetMinPoint(m_pntMouseDown, pMouse);

                //the size of the reactangle, again taking into account negative
situations
                s = new Size(Math.Abs(m_pntMouseDown.X - pMouse.X),
Math.Abs(m_pntMouseDown.Y - pMouse.Y));

                //the rectangle which indicates the selectin size
                m_rectSelection = new Rectangle(pSource, s);

                //redraw the image
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Pen p = new Pen(Color.Red);

            //make sure this gets disposed
            using(p)
            {
                //paint the image
                base.OnPaint (e);

                //if the mouse is currently down, draw the rectangle
                if(m_blnMouseDown)
                {
                    e.Graphics.DrawRectangle(p, m_rectSelection);
                }
            }
        }


        /// <summary>
        /// Gets the minimum point of two arbitrary points
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private Point GetMinPoint(Point p1, Point p2)
        {
            Point pMin = new Point();

            pMin.X = (p1.X < p2.X) ? p1.X : p2.X;
            pMin.Y = (p1.Y < p2.Y) ? p1.Y : p2.Y;

            return pMin;
        }
    }


Hope that helps

Mark R. Dawson




Show quoteHide quote
"Johnny H" wrote:

> Using: ms framework 1.1
>
> How to mark a selected region in a picturebox control?
>
>
> Thanks IA
>
> JH
>
>
Are all your drivers up to date? click for free checkup

Author
15 Aug 2005 11:59 PM
Johnny H
Mark R. Dawson wrote:
> Hi Johnny,
>   if you want to draw a rectangle like a selection region in your picturebox
> you could create your own picturebox control that inherits from the standard
> PictureBox and then override the Paint method to add drawing a rectangle,
> like:
>
>

Thank you for your extensive answer.

JH
Author
14 Aug 2005 2:08 PM
Lloyd Dupont
I think you might find the class
ControlPaint
quite helpful.

--
If you're in a war, instead of throwing a hand grenade at the enemy, throw one of those small pumpkins. Maybe it'll make everyone think how stupid war is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.
Show quoteHide quote
"Johnny H" <nospam@nowhere.net> wrote in message news:eD%23IzQKoFHA.2772@TK2MSFTNGP10.phx.gbl...
> Using: ms framework 1.1
>
> How to mark a selected region in a picturebox control?
>
>
> Thanks IA
>
> JH
>

Bookmark and Share