Using: ms framework 1.1

How to mark a selected region in a picturebox control?


Thanks IA

JH

RE: How to draw a focus rect? by MarkRDawson

MarkRDawson
Sun Aug 14 07:34:19 CDT 2005

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




"Johnny H" wrote:

> Using: ms framework 1.1
>
> How to mark a selected region in a picturebox control?
>
>
> Thanks IA
>
> JH
>
>

Re: How to draw a focus rect? by Lloyd

Lloyd
Sun Aug 14 09:08:20 CDT 2005

This is a multi-part message in MIME format.

------=_NextPart_000_0109_01C5A12D.71C1E560
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I think you might find the class
ControlPaint=20
quite helpful.

--=20
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.
"Johnny H" <nospam@nowhere.net> wrote in message =
news:eD%23IzQKoFHA.2772@TK2MSFTNGP10.phx.gbl...
> Using: ms framework 1.1
>=20
> How to mark a selected region in a picturebox control?
>=20
>=20
> Thanks IA
>=20
> JH
>
------=_NextPart_000_0109_01C5A12D.71C1E560
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2722" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>I think you might find the =
class</FONT></DIV>
<DIV><FONT face=3DArial color=3D#800000 =
size=3D2><STRONG>ControlPaint</STRONG>=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>quite helpful.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><BR><FONT face=3DArial =
size=3D2>-- <BR>If you're=20
in a war, instead of throwing a hand grenade at the enemy, throw one of =
those=20
small pumpkins. Maybe it'll make everyone think how stupid war is, and =
while=20
they are thinking, you can throw a real grenade at them.<BR>Jack=20
Handey.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>"Johnny H" &lt;</FONT><A=20
href=3D"mailto:nospam@nowhere.net"><FONT face=3DArial=20
size=3D2>nospam@nowhere.net</FONT></A><FONT face=3DArial size=3D2>&gt; =
wrote in=20
message </FONT><A =
href=3D"news:eD%23IzQKoFHA.2772@TK2MSFTNGP10.phx.gbl"><FONT=20
face=3DArial =
size=3D2>news:eD%23IzQKoFHA.2772@TK2MSFTNGP10.phx.gbl</FONT></A><FONT=20
face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; =
Using: ms=20
framework 1.1<BR>&gt; <BR>&gt; How to mark a selected region in a =
picturebox=20
control?<BR>&gt; <BR>&gt; <BR>&gt; Thanks IA<BR>&gt; <BR>&gt;=20
JH<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_0109_01C5A12D.71C1E560--


Re: How to draw a focus rect? by Johnny

Johnny
Mon Aug 15 18:59:24 CDT 2005

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