PC Review


Reply
Thread Tools Rate Thread

How to draw a focus rect?

 
 
Johnny H
Guest
Posts: n/a
 
      14th Aug 2005
Using: ms framework 1.1

How to mark a selected region in a picturebox control?


Thanks IA

JH

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      14th Aug 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
>
>

 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      14th Aug 2005
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.
"Johnny H" <(E-Mail Removed)> wrote in message news:eD%(E-Mail Removed)...
> Using: ms framework 1.1
>
> How to mark a selected region in a picturebox control?
>
>
> Thanks IA
>
> JH
>

 
Reply With Quote
 
Johnny H
Guest
Posts: n/a
 
      16th Aug 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the focus rect of a button Toby Microsoft Dot NET Framework Forms 0 28th May 2007 11:30 AM
Hiding button focus rect Kimmo Laine Microsoft C# .NET 0 15th Feb 2007 02:13 PM
disable focus rect on button Marco Microsoft C# .NET 0 19th May 2005 10:40 PM
using g.DrawRectangle(pen,rect) to draw a rectangle on an object Colin McGuire Microsoft VB .NET 3 31st Dec 2003 05:03 PM
Draw single line text in a rect Kay L. Microsoft VC .NET 2 16th Dec 2003 12:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:11 PM.