Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

How to draw a focus rect?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny H - 14 Aug 2005 08:45 GMT
Using: ms framework 1.1

How to mark a selected region in a picturebox control?

Thanks IA

JH
Mark R. Dawson - 14 Aug 2005 13:34 GMT
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

> Using: ms framework 1.1
>
[quoted text clipped - 3 lines]
>
> JH
Johnny H - 16 Aug 2005 00:59 GMT
> 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
Lloyd Dupont - 14 Aug 2005 15:08 GMT
I think you might find the class
ControlPaint
quite helpful.

Signature

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.

> Using: ms framework 1.1
>
[quoted text clipped - 3 lines]
>
> JH

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.