Click here to Skip to main content
16,021,041 members
Articles / Programming Languages / C#
Article

RectTracker's TrackRubberBand method in C#

Rate me:
Please Sign up or sign in to vote.
1.47/5 (7 votes)
7 Jun 2006 31.3K   872   11   2
RectTracker's TrackRubberBand method in C# (like MFC CRectTracker's TrackRubberBand)

Sample image

Introduction

A rubber band or focus rectangle is a rectangle that tracks with the mouse pointer while you hold down the left mouse button. The TrackRubberBand method of MFC CRectTracker class allows developers to do that. But .NET has not. I write a simple class RectTraker in C#. It has a same method named TrackRubberBand to help developer track a rectangle rubber band in any .net windows. I hope it's helpful to you. Main source code:

C#
public bool TrackRubberBand(Control control, 
            Point point, bool allowInvert)
{
    bool ok = true;

    rect = new Rectangle(point.X, point.Y, 
                         point.X, point.Y);

    Graphics g = control.CreateGraphics();

    Point pt1, pt2;
    pt1 = control.PointToScreen(point);
    pt2 = pt1;

    DrawRectangle(pt1, pt2);

    WinAPI.SetCapture(control.Handle);
    while (true)
    {
        MSG msg = new MSG();
        WinAPI.GetMessage(msg, IntPtr.Zero, 0, 0);

        switch (msg.message)
        {
            case WM_MOUSEMOVE:
            {
                DrawRectangle(pt1, pt2);

                if (allowInvert)
                    pt2 = new Point(msg.x, msg.y);
                else
                    pt2 = new Point(Math.Max(pt1.X, msg.x), 
                                    Math.Max(pt1.Y, msg.y));

                pt2 = AdjustPt2(pt1, pt2);

                DrawRectangle(pt1, pt2);
                break;
            }

            case WM_RBUTTONUP:
            case WM_MBUTTONUP:
            case WM_LBUTTONUP:
            {
                goto ExitLoop;
            }

            case WM_KEYDOWN:
            {
                if (msg.wParam == VK_ESCAPE)
                {
                    ok = false;
                    goto ExitLoop;
                }
                break;
            }

            case WM_LBUTTONDOWN:
            case WM_MBUTTONDOWN:
            case WM_RBUTTONDOWN:
                break;
        }
    }

ExitLoop:
    DrawRectangle(pt1, pt2);

    WinAPI.ReleaseCapture();

    g.Dispose();

    if (ok)
        rect = new Rectangle(pt1, 
               new Size(pt2.X-pt1.X, pt2.Y-pt1.Y));

    return ok;
}

private void DrawRectangle(Point pt1, Point pt2)
{
    Rectangle rc = new Rectangle(pt1, 
                   new Size(pt2.X-pt1.X, pt2.Y-pt1.Y));
    ControlPaint.DrawReversibleFrame( rc, Color.Red, 
                                      FrameStyle.Dashed );
}

To use this class, just like below:

C#
private void Form1_MouseDown(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    RectTracker r = new RectTracker();
    Point pt = new Point(e.X, e.Y);
    r.TrackRubberBand(this, pt, true);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generali get the error position of rectangle Pin
pclion7-May-09 17:00
pclion7-May-09 17:00 
my code is:
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
tracker.TrackRubberBand(this, pt, checkBox1.CheckState == CheckState.Checked);
rect = tracker.Rect;
Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.Red), rect);
}

how i do get the right rectangle?

pclion

QuestionIs it necessary to handle message loop like this? Pin
Oliver Bock22-Apr-09 15:22
Oliver Bock22-Apr-09 15:22 

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.