C# Rect Tracker

來源:互聯網
上載者:User

本文轉自http://www.codeproject.com/KB/miscctrl/CSharpRectTracker.aspx

 

  • Download source code - 18.7 Kb

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 Collapse | Copy 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 Collapse | Copy Code
public void Mouse_Move(object sender,System.Windows.Forms.MouseEventArgs e)    {//minimum size for the control is 8x8if (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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.