How to draw a focus rect?

  • Thread starter Thread starter Johnny H
  • Start date Start date
J

Johnny H

Using: ms framework 1.1

How to mark a selected region in a picturebox control?


Thanks IA

JH
 
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
 
Mark said:
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
 
Back
Top