65.9K
CodeProject is changing. Read more.
Home

Using CRectTrackerCS in C#

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.11/5 (9 votes)

May 27, 2007

CPOL

1 min read

viewsIcon

39041

downloadIcon

697

Using the CRectTrackerCS in C# to create a rubber band.

Screenshot - CRectTrackerCS2.1_Demo.gif

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.

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