Click here to Skip to main content
15,867,851 members
Articles / Multimedia / GDI+
Article

C# Rect Tracker

Rate me:
Please Sign up or sign in to vote.
3.56/5 (23 votes)
16 Nov 20041 min read 161.9K   3.3K   54   29
C# Rect Tracker (like CRectTracker on C++).

Sample image

Introduction

Ever tried to find that object that allows Visual studio to move and resize controls? Well, I didn't find it in C# but in C++. Well, here it is in C# :)

Background

The project I was working on required me to create forms in run-time, so I needed something to edit controls - RectTracker.

In C++ there is an object called CRectTracker, but in C# there isn't, so I tried to clone it, it doesn't do what the Visual Studio Tracker does, but it does the job on resizing and moving controls on a form.

Methodology

It's made of a UserControl because doing it using the form graphics object creates ordering problems (a control is "above" the tracker), so making it a control itself, allows me to play with Z-Order and set the control to front.

I used Region Transparenting to create a place for the Control I want to move/resize.

Using The Code

C#
SomeControl.BringToFront();
SomeControl.Capture = false;
if(this.Controls.Contains(CSharpTracker))
  this.Controls.Remove(CSharpTracker);
CSharpTracker = new RectTracker(SomeControl);

this.Controls.Add(CSharpTracker);
CSharpTracker.BringToFront();
CSharpTracker.Draw();

Control Moving

C#
public void Mouse_Move(object sender,System.Windows.Forms.MouseEventArgs e)
    {
//minimum size for the control is 8x8
if (currentControl.Height < 8)
{
  currentControl.Height = 8;
  return;
}
else if (currentControl.Width < 8)
{
  currentControl.Width = 8;
  return;
}
    
switch(this.CurrBorder)
{
  case RESIZE_BORDER.RB_TOP:
    currentControl.Height = currentControl.Height - e.Y + prevLeftClick.Y;
    if (currentControl.Height > 8)
    currentControl.Top = currentControl.Top + e.Y - prevLeftClick.Y;    
    break;
  case RESIZE_BORDER.RB_TOPLEFT:
    currentControl.Height = currentControl.Height - e.Y + prevLeftClick.Y;
    if (currentControl.Height > 8)
    currentControl.Top =currentControl.Top + e.Y - prevLeftClick.Y;
    currentControl.Width = currentControl.Width - e.X + prevLeftClick.X;
    if (currentControl.Width > 8)
    currentControl.Left =currentControl.Left + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_TOPRIGHT:
    currentControl.Height = currentControl.Height - e.Y + prevLeftClick.Y;
    if (currentControl.Height > 8)
    currentControl.Top = currentControl.Top + e.Y - prevLeftClick.Y;
    currentControl.Width = currentControl.Width + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_RIGHT:
    currentControl.Width = currentControl.Width + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_BOTTOM:
    currentControl.Height = currentControl.Height + e.Y - prevLeftClick.Y;
    break;
  case RESIZE_BORDER.RB_BOTTOMLEFT:
    currentControl.Height = currentControl.Height + e.Y - prevLeftClick.Y;
    currentControl.Width = currentControl.Width - e.X + prevLeftClick.X;
    if (currentControl.Width > 8)
currentControl.Left = currentControl.Left + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_BOTTOMRIGHT:
    currentControl.Height = currentControl.Height + e.Y - prevLeftClick.Y;
    currentControl.Width = currentControl.Width + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_LEFT:
    currentControl.Width = currentControl.Width - e.X + prevLeftClick.X;
    if (currentControl.Width > 8)
    currentControl.Left = currentControl.Left + e.X - prevLeftClick.X;
    break;
  case RESIZE_BORDER.RB_NONE:
    currentControl.Location = new Point(currentControl.Location.X + e.X - 
    prevLeftClick.X, currentControl.Location.Y + e.Y - prevLeftClick.Y);
    break;
  
}

}

Conclusion

This SharpRectTracker control is a clone of the CRectTracker, and it allows a user to Move and Resize form objects (control) in run-time, this control can be further enhanced to allow multiple object move/resize (using selection) and to do selectable Resize (for an object that only the width can be changed).

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
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to select multiple objects Pin
TrushnaK19-Nov-13 19:43
TrushnaK19-Nov-13 19:43 
QuestionResize Pin
SPX8717-Jun-13 6:15
SPX8717-Jun-13 6:15 
Questionnot working the project. Pin
jishadshajahan3-Apr-13 0:36
jishadshajahan3-Apr-13 0:36 
GeneralMore Functionality For Rect Tracker Pin
Derek Hart12-Jan-08 13:41
Derek Hart12-Jan-08 13:41 
QuestionHow to make a perspective Pin
El Tucu17-Sep-06 15:52
El Tucu17-Sep-06 15:52 
GeneralScrollbars Pin
zulfi_k10-Mar-06 5:19
zulfi_k10-Mar-06 5:19 
GeneralRe: Scrollbars Pin
nashcontrol11-Mar-06 5:59
nashcontrol11-Mar-06 5:59 
GeneralRe: Scrollbars Pin
zulfi_k13-Mar-06 4:23
zulfi_k13-Mar-06 4:23 
GeneralRe: Scrollbars [modified] Pin
Pugwash200412-Jun-06 1:54
Pugwash200412-Jun-06 1:54 
GeneralVery Helpful Pin
kmerrell25-Feb-06 13:53
kmerrell25-Feb-06 13:53 
Generalthank you! Pin
nashcontrol9-Mar-06 10:19
nashcontrol9-Mar-06 10:19 
GeneralWorking with forms Pin
rleonardiBR29-Apr-05 8:02
rleonardiBR29-Apr-05 8:02 
GeneralI doubt it can do it Pin
nashcontrol6-May-05 4:03
nashcontrol6-May-05 4:03 
GeneralSelect several controls Pin
Alexander_Vikhorev7-Mar-05 7:07
Alexander_Vikhorev7-Mar-05 7:07 
GeneralRe: Select several controls Pin
Alexander_Vikhorev7-Mar-05 7:09
Alexander_Vikhorev7-Mar-05 7:09 
GeneralRe: Select several controls Pin
nashcontrol11-Mar-05 0:04
nashcontrol11-Mar-05 0:04 
GeneralListBox Pin
villander22-Dec-04 8:00
villander22-Dec-04 8:00 
GeneralRe: ListBox Pin
nashcontrol11-Mar-05 0:57
nashcontrol11-Mar-05 0:57 
GeneralRectTracker improvements Pin
klaus3b7-Dec-04 2:41
klaus3b7-Dec-04 2:41 
Generalgreat!, thanks Pin
nashcontrol10-Mar-05 22:06
nashcontrol10-Mar-05 22:06 
GeneralDesigner Pin
Gaston23-Nov-04 0:20
Gaston23-Nov-04 0:20 
GeneralRe: Designer Pin
Testo28ß515-Oct-06 7:43
Testo28ß515-Oct-06 7:43 
GeneralNice Work, But... Pin
Aju.George10-Nov-04 3:09
Aju.George10-Nov-04 3:09 
GeneralRe: Nice Work, But... Pin
nashcontrol10-Nov-04 4:17
nashcontrol10-Nov-04 4:17 
Generalabout the link you gave Pin
nashcontrol10-Nov-04 4:31
nashcontrol10-Nov-04 4:31 

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.