在mousedown中建立Rubberband类的对象 mousemove中调用该对象的ResizeTo 在MouseUp中调用该对象的End public class Rubberband { protected Control parent; protected Rectangle rect = Rectangle.Empty; public Rubberband(Control theParent, Point theStartingPoint) { parent = theParent; parent.Capture = true; Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle); rect = new Rectangle(theStartingPoint.X, theStartingPoint.Y, 0, 0); } public void End() { Cursor.Clip = Rectangle.Empty; parent.Capture = false; // erase the rubberband Draw(); rect = Rectangle.Empty; } public void ResizeTo(Point thePoint) { // erase the old rubberband Draw(); // get the new size of the rubberband rect.Width = thePoint.X - rect.Left; rect.Height = thePoint.Y - rect.Top; // draw the new rubberband Draw(); } public Rectangle Bounds() { // return a normalized rectangle, i.e. a rect // where (left <= right) and (top <= bottom) if ( (rect.Left > rect.Right) || (rect.Top > rect.Bottom) ) { int left = Math.Min(rect.Left, rect.Right); int right = Math.Max(rect.Left, rect.Right); int top = Math.Min(rect.Top, rect.Bottom); int bottom = Math.Max(rect.Top, rect.Bottom); return Rectangle.FromLTRB(left, top, right, bottom); } return rect; } // Reversible drawing method // Calling theis method the first time draws the rubberband. // Calling it a second time with the same rect erases the rubberband protected void Draw() { Rectangle r = parent.RectangleToScreen(rect); ControlPaint.DrawReversibleFrame(r, Color.White, FrameStyle.Dashed); } }
public class Rubberband { protected Control parent; protected Rectangle rect = Rectangle.Empty; public Rubberband(Control theParent, Point theStartingPoint) { parent = theParent; parent.Capture = true; Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle); rect = new Rectangle(theStartingPoint.X, theStartingPoint.Y, 0, 0); } public void End() { Cursor.Clip = Rectangle.Empty; parent.Capture = false; // erase the rubberband Draw(); rect = Rectangle.Empty; } public void ResizeTo(Point thePoint) { // erase the old rubberband Draw(); // get the new size of the rubberband rect.Width = thePoint.X - rect.Left; rect.Height = thePoint.Y - rect.Top; // draw the new rubberband Draw(); } public Rectangle Bounds() { // return a normalized rectangle, i.e. a rect // where (left <= right) and (top <= bottom) if ( (rect.Left > rect.Right) || (rect.Top > rect.Bottom) ) { int left = Math.Min(rect.Left, rect.Right); int right = Math.Max(rect.Left, rect.Right); int top = Math.Min(rect.Top, rect.Bottom); int bottom = Math.Max(rect.Top, rect.Bottom); return Rectangle.FromLTRB(left, top, right, bottom); } return rect; } // Reversible drawing method // Calling theis method the first time draws the rubberband. // Calling it a second time with the same rect erases the rubberband protected void Draw() { Rectangle r = parent.RectangleToScreen(rect); ControlPaint.DrawReversibleFrame(r, Color.White, FrameStyle.Dashed); } }
mousemove中调用该对象的ResizeTo
在MouseUp中调用该对象的End public class Rubberband
{
protected Control parent;
protected Rectangle rect = Rectangle.Empty; public Rubberband(Control theParent, Point theStartingPoint)
{
parent = theParent;
parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
rect = new Rectangle(theStartingPoint.X, theStartingPoint.Y, 0, 0);
} public void End()
{
Cursor.Clip = Rectangle.Empty;
parent.Capture = false; // erase the rubberband
Draw();
rect = Rectangle.Empty;
} public void ResizeTo(Point thePoint)
{
// erase the old rubberband
Draw(); // get the new size of the rubberband
rect.Width = thePoint.X - rect.Left;
rect.Height = thePoint.Y - rect.Top; // draw the new rubberband
Draw();
} public Rectangle Bounds()
{
// return a normalized rectangle, i.e. a rect
// where (left <= right) and (top <= bottom)
if ( (rect.Left > rect.Right) || (rect.Top > rect.Bottom) )
{
int left = Math.Min(rect.Left, rect.Right);
int right = Math.Max(rect.Left, rect.Right);
int top = Math.Min(rect.Top, rect.Bottom);
int bottom = Math.Max(rect.Top, rect.Bottom);
return Rectangle.FromLTRB(left, top, right, bottom);
}
return rect;
} // Reversible drawing method
// Calling theis method the first time draws the rubberband.
// Calling it a second time with the same rect erases the rubberband
protected void Draw()
{
Rectangle r = parent.RectangleToScreen(rect);
ControlPaint.DrawReversibleFrame(r, Color.White, FrameStyle.Dashed);
}
}
http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/SDaskgui02172004.mspx
{
protected Control parent;
protected Rectangle rect = Rectangle.Empty; public Rubberband(Control theParent, Point theStartingPoint)
{
parent = theParent;
parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
rect = new Rectangle(theStartingPoint.X, theStartingPoint.Y, 0, 0);
} public void End()
{
Cursor.Clip = Rectangle.Empty;
parent.Capture = false; // erase the rubberband
Draw();
rect = Rectangle.Empty;
} public void ResizeTo(Point thePoint)
{
// erase the old rubberband
Draw(); // get the new size of the rubberband
rect.Width = thePoint.X - rect.Left;
rect.Height = thePoint.Y - rect.Top; // draw the new rubberband
Draw();
} public Rectangle Bounds()
{
// return a normalized rectangle, i.e. a rect
// where (left <= right) and (top <= bottom)
if ( (rect.Left > rect.Right) || (rect.Top > rect.Bottom) )
{
int left = Math.Min(rect.Left, rect.Right);
int right = Math.Max(rect.Left, rect.Right);
int top = Math.Min(rect.Top, rect.Bottom);
int bottom = Math.Max(rect.Top, rect.Bottom);
return Rectangle.FromLTRB(left, top, right, bottom);
}
return rect;
} // Reversible drawing method
// Calling theis method the first time draws the rubberband.
// Calling it a second time with the same rect erases the rubberband
protected void Draw()
{
Rectangle r = parent.RectangleToScreen(rect);
ControlPaint.DrawReversibleFrame(r, Color.White, FrameStyle.Dashed);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (rubberband != null)
rubberband.ResizeTo(new Point(e.X,e.Y));
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
this.rubberband = new Rubberband(this,new Point(e.X,e.Y));
}private void Form1_MouseUp(object sender,MouseEventArgs e)
{
if (rubberband != null)
{ rubberband.End();
rubberband = null;
}}