Skip to main content
Email Password   helpLost your password?

Please note: The large size of the downloads is caused by the images included with the demo project. The compiled .dll library is only 44Kb.

Sample Image - TdhMarchingAnts.jpg

Introduction

As a minor part of the image processing sub-system of my project I needed a way to allow users to select an identifying region on the set of images they are working with. This region for each individual image is then to be shown at magnification (as compared to the full/cropped images shown as thumbnails).

While my need was very specific -- I wanted to know the co-ordinates of a rectangle selected on the surface of a PictureBox control -- I decided to write a more versatile class which should be able to draw a rectangle on any object which inherits from System.Windows.Forms.Control and return the rectangle co-ordinates to the invoking program.

The class I wrote to accomplish this goal is: TdhMarchingAnts_NativeWindow. This class may be used by instantiating it directly via code.

I also wrote a component class, TdhMarchingAnts, to act as a development-environment "wrapper" for TdhMarchingAnts_NativeWindow.

The demo program uses/tests both classes.

Using The Classes -- The Code

To use the TdhMarchingAnts classes as I've written them, add a reference in your project to the class library 'TDHMarchingAnts.dll.' The namespace used in this library is:

using TDHControls.TDHMarchingAnts;

An example of 'Windows Form Designer generated code' for the TdhMarchingAnts component is:

private void InitializeComponent()
{

    this.tdhAnts 
        = new TDHControls.TDHMarchingAnts.TdhMarchingAnts(this.components);


    //
    // tdhAnts 
    //
    this.tdhAnts.AntAttachedControl = this.pnlDemo;
    this.tdhAnts.AntBackColor = System.Drawing.Color.Black;	// default: .White
    this.tdhAnts.AntForeColor = System.Drawing.Color.White;	// default: .Red
    this.tdhAnts.AntsActive = true;			// Note: this is 'false' by default 
    this.tdhAnts.RegionDeselectEvent += new 
       TDHControls.TDHMarchingAnts.RegionDeselectEventHandler(this.tdhAnts_RegionDeselectEvent);
    this.tdhAnts.RegionSelectEvent += new 
        TDHControls.TDHMarchingAnts.RegionSelectEventHandler(this.tdhAnts_RegionSelectEvent);


}

Example code for instantiating the TdhMarchingAnts_NativeWindow class independently of the TdhMarchingAnts component is:

 
private TDHControls.TDHMarchingAnts.TdhMarchingAnts_NativeWindow native_PicBox;
private TDHControls.TDHMarchingAnts.TdhMarchingAnts_NativeWindow native_Panel;
 
 
{

    this.native_PicBox 
        = new TDHControls.TDHMarchingAnts.TdhMarchingAnts_NativeWindow(this.pbxImage);
    this.native_PicBox.AntsActive = true;		// Note: this is 'false' by default 
    this.native_PicBox.RegionSelectEvent += new 
        TDHControls.TDHMarchingAnts.RegionSelectEventHandler(this._PicBox_RegionSelectEvent);
    this.native_PicBox.RegionDeselectEvent += new
        TDHControls.TDHMarchingAnts.RegionDeselectEventHandler(this._PicBox_RegionDeselectEvent);

    // The Constructor also accepts the "left-Ant" fore- and back- colors 
    this.native_Panel 
        = new TDHControls.TDHMarchingAnts.TdhMarchingAnts_NativeWindow
        (this.pnlForSomething, Color.Blue, Color.Yellow);

}

Using The Classes -- The Code (The EventHandlers)

The TdhMarchingAnts_NativeWindow class and the TdhMarchingAnts component raise potentially six events (that is, three each for either mouse button).

Assuming the appropriate boolean properties are true, the event _RegionSelectEvent() (and corresponding event _RightRegionSelectEvent()) is raised when the left (or right) mouse button is released following a "click-and-drag."

Assuming the appropriate boolean properties are true, the events _ClickEvent() and _RegionDeselectEvent() (and corresponding events _RightClickEvent() and _RightRegionDeselectEvent()) are raised when the left (or right) mouse button is clicked. Thus, _ClickEvent() (or _RightClickEvent()) may be raised either alone or in conjuction with _RegionDeselectEvent() (or _RightRegionDeselectEvent()).

The signatures of the event handler methods are as follows:

 
private void _ClickEvent(object sender, System.Drawing.Point ClickedPoint)
{
    // do something (assuming you care about this event)
}

private void _RegionDeselectEvent(object sender, System.Drawing.Point ClickedPoint)
{
    // do something (assuming you care about this event)
}

private void _RegionSelectEvent(object sender, System.Drawing.Rectangle SelectedRect)
{
    // do something (surely you do care about this event!)
}

private void _RightClickEvent(object sender, System.Drawing.Point ClickedPoint)
{
    // do something (assuming you care about this event)
}

private void _RightRegionDeselectEvent(object sender, System.Drawing.Point ClickedPoint)
{
    // do something (assuming you care about this event)
}

private void _RightRegionSelectEvent(object sender, System.Drawing.Rectangle SelectedRect)
{
    // do something (surely you do care about this event!)
}
Please note: With version 1.0.004, the signatures of all the event handler methods have been changed (I apologize if this causes an inconvenience). The event handler parameter-lists should have included "object sender" originally.

Using The Classes -- The Interface

Beginning with version 1.0.002, the TdhMarchingAnts classes may be used with either the left mouse button or the right (or both). The "default" button is the left: that is, the previously named properties, methods and events are defined for mouse left-button activity; the corresponding properties, methods and events defined for mouse right-button activity are preceded with the word "Right_" (for properties and methods) or "Right" (for events).

Both the TdhMarchingAnts component and the TdhMarchingAnts_NativeWindow class have essentially the same public interface:

  • Public (static) methods limited to the TdhMarchingAnts component class are:
  • "Global" properties of both classes are:
  • "Left-button" properties, methods, and eventhandler signatures of both classes are:
  • "Right-button" properties, methods, and eventhandler signatures of both classes are:

    History

  • You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralHow can I lock the aspect ratio of selected rectangle? Pin
    peste khandan
    0:27 18 Oct '09  
    GeneralRe: How can I lock the aspect ratio of selected rectangle? Pin
    Ilíon
    6:39 21 Oct '09  
    AnswerRe: How can I lock the aspect ratio of selected rectangle? Pin
    Ilíon
    12:43 21 Oct '09  
    GeneralAnswer, part 1 Pin
    Ilíon
    16:02 23 Oct '09  
    GeneralAnswer, part 2 [modified] Pin
    Ilíon
    5:00 24 Oct '09  
    GeneralRe: Answer, part 2 Pin
    peste khandan
    1:09 29 Oct '09  
    GeneralRe: Answer, part 2 Pin
    Ilíon
    7:49 29 Oct '09  
    GeneralVery Nice Work Pin
    Oakman
    11:02 8 Jun '08  
    GeneralPort to ASP? Pin
    Member 4536460
    4:57 13 Feb '08  
    GeneralRe: Port to ASP? Pin
    Ilíon
    5:06 13 Feb '08  
    GeneralYour real name? Pin
    Marakai
    18:35 11 Jun '07  
    GeneralRe: Your real name? Pin
    Ilíon
    21:54 11 Jun '07  
    GeneralRe: Your real name? Pin
    Marakai
    21:58 11 Jun '07  
    GeneralRe: Your real name? Pin
    Ilíon
    5:27 12 Jun '07  
    GeneralRe: Your real name? Pin
    Marakai
    3:00 13 Jun '07  
    GeneralI love you guy !!! Pin
    overpeer
    7:53 28 Jan '07  
    GeneralRe: I love you guy !!! Pin
    Ilíon
    4:56 29 Jan '07  
    GeneralInvalidOperationException Pin
    Albert G.
    8:40 11 Jan '07  
    GeneralRe: InvalidOperationException [modified] Pin
    Ilíon
    11:36 11 Jan '07  
    GeneralRe: InvalidOperationException Pin
    Albert G.
    6:42 12 Jan '07  
    GeneralRe: InvalidOperationException Pin
    Ilíon
    7:58 12 Jan '07  
    GeneralRe: InvalidOperationException Pin
    Albert G.
    11:00 13 Jan '07  
    GeneralRe: InvalidOperationException [modified] Pin
    Ilíon
    7:28 16 Jan '07  
    GeneralBUG Pin
    Sacha Barber
    6:41 8 Jan '07  
    GeneralRe: BUG Pin
    Ilíon
    10:13 8 Jan '07  


    Last Updated 8 Jan 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009