
Introduction
I am a novice in C# and OOP. I found that the TrackRubberBand method of the MFC CRectTracker class is not available in .NET. So I wrote a simple class RectTracker in C# and I called it CRectTrackerCS. I hope it is helpful to you.
To throw out a minnow to catch a whale. If you find problems and bugs in this code, please tell me about it. Thanks!
Using the code
CRectTrackerCS 2.1 is very easy to use.
- Create an instance and initialize it:
CRectTrackerCS TestRubberBand = new CRectTrackerCS();
TestRubberBand.Create();
- On the
MouseDown event:
TestRubberBand.StartPoint(this, e);
- On
MouseUp event:
TestRubberBand.EndPoint(this, e);
- On
MouseMove event:
TestRubberBand.TrackRubberBand(this, e);
- On
Paint event:
TestRubberBand.DrawRubberBand(this, e);
- And last, don't forget to free up the resources:
TestRubberBand.Destroy();
CRectTrackerCS has four attributes (Read Only): X, Y, Width, Height. Add a StatusStrip control, and in the MouseMove event:
toolStripStatusLabel1.Text = "RubberBand:"+" X="+
TestRect.X.ToString() + " Y="+TestRect.Y.ToString() +
" Width="+TestRect.Width.ToString() +
" Height="+TestRect.Height.ToString();
The StatusStrip control will now show the rubber band's attributes.
Tip: If you want the rubber band streamed in a Form, you need to add a Timer control and in the Tick event:
this.Invalidate(null, true);
or use InvalidateRectangle(), which is a new method in CRectTrackerCS 2.1:
this.Invalidate(TestRubberBand.InvalidateRectangle(), true);
CRectTrackerCS 2.1 --- What's new
Three methods have been added: InvalidateRectangle, SetRubberBandStyle (+2 overloads), and LoadDefaultRubberBandStyle. You can change the style and color of the rubber band and the ResizePin.
Here are the StyleFlags of CRectTrackerCS 2.1: ResizePinNoHide, ResizePinAutoHide, ResizePinAlwaysHide, SolidLine, DottedLine.
That's it!
All suggestions, ideas, bug reports, and criticisms are welcome!
Other info
Testing environment:
- WinXP (Professional SP2)
- Microsoft Visual Studio 2005 (Installed edition: C# Express)
- Version 8.0.50727.762 (SP.050727-7600)
- Microsoft .NET Framework Version 2.0.50727
My blog's address: http://timw.yeah.net, http://timw.126.com.
History
- CRectTrackerCS 2.1 18:17 2007-06-01
- CRectTrackerCS 2.0 17:08 2007-05-31
- CRectTrackerCS 1.0 23:28 2007-05-26