Click here to Skip to main content
Licence 
First Posted 9 Nov 2004
Views 98,107
Downloads 1,470
Bookmarked 47 times

C# Rect Tracker

By | 16 Nov 2004 | Article
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

    
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

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

About the Author

nashcontrol

Web Developer
Indigo - Smart House Solutions
Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMore Functionality For Rect Tracker PinmemberDerek Hart13:41 12 Jan '08  
QuestionHow to make a perspective PinmemberEl Tucu15:52 17 Sep '06  
GeneralScrollbars Pinmemberzulfi_k5:19 10 Mar '06  
Just having a slight problem. When you select a control and move it to an area where scrollbars will appear it places it fine. But when you reselect the the control and start moving it the location of the control changes (it looses the scrollbar offset i think). Its using the mouse coordinates as the appear on the screen and not the forms cooridinates. I've tried to change it but with no luck. Any ideas on how to fix this?
 
Thanks.
GeneralRe: Scrollbars Pinmembernashcontrol5:59 11 Mar '06  
GeneralRe: Scrollbars Pinmemberzulfi_k4:23 13 Mar '06  
GeneralRe: Scrollbars [modified] PinmemberPugwash20041:54 12 Jun '06  
GeneralVery Helpful Pinmemberkmerrell13:53 25 Feb '06  
Generalthank you! Pinmembernashcontrol10:19 9 Mar '06  
GeneralWorking with forms PinmemberrleonardiBR8:02 29 Apr '05  
GeneralI doubt it can do it Pinmembernashcontrol4:03 6 May '05  
GeneralSelect several controls PinmemberAlexander_Vikhorev7:07 7 Mar '05  
GeneralRe: Select several controls PinmemberAlexander_Vikhorev7:09 7 Mar '05  
GeneralRe: Select several controls Pinmembernashcontrol0:04 11 Mar '05  
GeneralListBox Pinmembervillander8:00 22 Dec '04  
GeneralRe: ListBox Pinmembernashcontrol0:57 11 Mar '05  
GeneralRectTracker improvements Pinmemberklaus3b2:41 7 Dec '04  
Generalgreat!, thanks Pinmembernashcontrol22:06 10 Mar '05  
GeneralDesigner PinmemberGaston0:20 23 Nov '04  
GeneralRe: Designer PinmemberTesto28ß57:43 15 Oct '06  
GeneralNice Work, But... PinmemberAju.George3:09 10 Nov '04  
GeneralRe: Nice Work, But... Pinmembernashcontrol4:17 10 Nov '04  
Generalabout the link you gave Pinmembernashcontrol4:31 10 Nov '04  
Generaldoh: PinsussAnonymous0:42 10 Nov '04  
GeneralDataGrid PinmemberGary Thom23:32 9 Nov '04  
GeneralYes Pinmembernashcontrol23:59 9 Nov '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 16 Nov 2004
Article Copyright 2004 by nashcontrol
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid