Click here to Skip to main content
15,606,438 members
Articles / Programming Languages / C#
Posted 26 May 2007


16 bookmarked

Using CRectTrackerCS in C#

Rate me:
Please Sign up or sign in to vote.
2.11/5 (9 votes)
5 Dec 2007CPOL1 min read
Using the CRectTrackerCS in C# to create a rubber band.

Screenshot - CRectTrackerCS2.1_Demo.gif


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.

  1. Create an instance and initialize it:
  2. C#
    CRectTrackerCS TestRubberBand = new CRectTrackerCS();
  3. On the MouseDown event:
  4. C#
    TestRubberBand.StartPoint(this, e);
  5. On MouseUp event:
  6. C#
    TestRubberBand.EndPoint(this, e);
  7. On MouseMove event:
  8. C#
    TestRubberBand.TrackRubberBand(this, e);
  9. On Paint event:
  10. C#
    TestRubberBand.DrawRubberBand(this, e);
  11. And last, don't forget to free up the resources:
  12. C#

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:,


  • CRectTrackerCS 2.1 18:17 2007-06-01
  • CRectTrackerCS 2.0 17:08 2007-05-31
  • CRectTrackerCS 1.0 23:28 2007-05-26


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
China China
good luck!

Comments and Discussions

Generalnot able to download, can you provide another link, thank Pin
jalal_haddad11-Apr-10 6:35
jalal_haddad11-Apr-10 6:35 
Generalwell done! Pin
some_zeng13-Feb-09 23:31
some_zeng13-Feb-09 23:31 
Generalgood job. Pin
BUGHOHO26-May-07 22:50
BUGHOHO26-May-07 22:50 
GeneralRe: good job. Pin
ZhaoChunsheng26-May-07 23:12
ZhaoChunsheng26-May-07 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.