Click here to Skip to main content
Click here to Skip to main content

Using CRectTrackerCS in C#

, 5 Dec 2007
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

good luck!

Comments and Discussions

 
Generalnot able to download, can you provide another link, thank Pinmemberjalal_haddad11-Apr-10 6:35 
Generalwell done! Pinmembersome_zeng13-Feb-09 23:31 
Smile | :)
Generalgood job. PinmemberBUGHOHO26-May-07 22:50 
GeneralRe: good job. PinmemberZhaoChunsheng26-May-07 23:12 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 6 Dec 2007
Article Copyright 2007 by ZhaoChunsheng
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid