1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
| // Copy Right 2004 - feel free to do anything with this code.
//
// this project was created using the methods shown in:
// Creating Bitmap Regions for Forms and Buttons - http://www.thecodeproject.com/csharp/bmprgnform.asp
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace RectTrackerSharp
{
/// <summary>
/// Summary description for RectTracker.
/// </summary>
public class RectTracker : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
//original rect
Rectangle baseRect;
//new rect with bounds
Rectangle ControlRect;
//all small rects
Rectangle[] SmallRect = new Rectangle[8];
//sqare size
Size Sqare = new Size(6,6);
//graphics object
Graphics g;
//the control that is tracked
Control currentControl;
Rectangle currentRectangle;
// To store the location of previous mouse left click in the form
// so that we can use it to calculate the new form location during dragging
private Point prevLeftClick;
// To determine if it is the first time entry for every dragging of the form
private bool isFirst = true;
//defines the color surrounding the tracker
Color MyBackColor = Color.Wheat;
//defines the color of the sqares
Color SqareColor = Color.White;
//defines the line color of the sqares
Color SqareLineColor = Color.Black;
//The Control the tracker is used on
public Control Control
{
get {return currentControl;}
set
{
//currentControl = value;
//Rect = currentControl.Bounds;
}
}
/// <summary>
/// keep track of which border of the box is to be resized.
/// </summary>
enum RESIZE_BORDER
{
RB_NONE = 0,
RB_TOP = 1,
RB_RIGHT = 2,
RB_BOTTOM = 3,
RB_LEFT = 4,
RB_TOPLEFT = 5,
RB_TOPRIGHT = 6,
RB_BOTTOMLEFT = 7,
RB_BOTTOMRIGHT = 8
}
//currently resize mouse location
private RESIZE_BORDER CurrBorder;
/// <summary>
/// The current position of the rectangle in client coordinates (pixels).
/// </summary>
public Rectangle Rect
{
get {return baseRect;}
set
{
int X = Sqare.Width;
int Y = Sqare.Height;
int Height = value.Height;
int Width = value.Width;
baseRect = new Rectangle(X,Y,Width,Height);
SetRectangles();
}
}
/// <summary>
/// Set the Sqares positions
/// </summary>
void SetRectangles()
{
//TopLeft
SmallRect[0] = new Rectangle(new Point(baseRect.X - Sqare.Width,baseRect.Y - Sqare.Height),Sqare);
//TopRight
SmallRect[1] = new Rectangle(new Point(baseRect.X + baseRect.Width,baseRect.Y - Sqare.Height),Sqare);
//BottomLeft
SmallRect[2] = new Rectangle(new Point(baseRect.X - Sqare.Width,baseRect.Y + baseRect.Height),Sqare);
//BottomRight
SmallRect[3] = new Rectangle(new Point(baseRect.X + baseRect.Width,baseRect.Y + baseRect.Height),Sqare);
//TopMiddle
SmallRect[4] = new Rectangle(new Point(baseRect.X + (baseRect.Width/2) - (Sqare.Width/2) ,baseRect.Y - Sqare.Height),Sqare);
//BottomMiddle
SmallRect[5] = new Rectangle(new Point(baseRect.X + (baseRect.Width/2) - (Sqare.Width/2) ,baseRect.Y + baseRect.Height),Sqare);
//LeftMiddle
SmallRect[6] = new Rectangle(new Point(baseRect.X - Sqare.Width,baseRect.Y + (baseRect.Height/2) - (Sqare.Height/2)),Sqare);
//RightMiddle
SmallRect[7] = new Rectangle(new Point(baseRect.X + baseRect.Width,baseRect.Y + (baseRect.Height/2) - (Sqare.Height/2)),Sqare);
//the whole tracker rect
ControlRect = new Rectangle(new Point(0,0),this.Bounds.Size);
}
/// <summary>
/// Draws Sqares
/// </summary>
public void Draw()
{
try
{
//draw sqares
g.FillRectangles(Brushes.White,SmallRect);
//fill sqares
g.DrawRectangles(Pens.Black,SmallRect);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Check point position to see if it's on tracker
/// </summary>
/// <param name="point">Current Point Position</param>
/// <returns></returns>
public bool Hit_Test(Point point)
{
//Check if the point is somewhere on the tracker
if (!ControlRect.Contains(point))
{
//should never happen
Cursor.Current = Cursors.Arrow;
return false;
}
else if(SmallRect[0].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
CurrBorder = RESIZE_BORDER.RB_TOPLEFT;
}
else if(SmallRect[3].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
CurrBorder = RESIZE_BORDER.RB_BOTTOMRIGHT;
}
else if(SmallRect[1].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
CurrBorder = RESIZE_BORDER.RB_TOPRIGHT;
}
else if(SmallRect[2].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
CurrBorder = RESIZE_BORDER.RB_BOTTOMLEFT;
}
else if(SmallRect[4].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
CurrBorder = RESIZE_BORDER.RB_TOP;
}
else if(SmallRect[5].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
CurrBorder = RESIZE_BORDER.RB_BOTTOM;
}
else if(SmallRect[6].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
CurrBorder = RESIZE_BORDER.RB_LEFT;
}
else if(SmallRect[7].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
CurrBorder = RESIZE_BORDER.RB_RIGHT;
}
else if(ControlRect.Contains(point))
{
Cursor.Current = Cursors.SizeAll;
CurrBorder = RESIZE_BORDER.RB_NONE;
}
return true;
}
/// <summary>
/// Check point position to see if it's on tracker
/// </summary>
/// <param name="x">X position</param>
/// <param name="y">Y position</param>
/// <returns></returns>
public bool Hit_Test(int x, int y)
{
return Hit_Test(new Point(x,y));
}
/// <summary>
/// Cunstructor
/// </summary>
/// <param name="theControl">The Control we want to use the Tracker on</param>
public RectTracker(Control theControl)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
//set the tracked control
currentControl = theControl;
//Create the tracker
Create();
}
public RectTracker(Rectangle theRectangle)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
//set the tracked control
currentRectangle = theRectangle;
//Create the tracker
Create();
}
public RectTracker()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
}
/// <summary>
/// Setup the tracker
/// </summary>
private void Create()
{
//create tracker bounds
int X = currentControl.Bounds.X - Sqare.Width;
int Y = currentControl.Bounds.Y - Sqare.Height;
int Height = currentControl.Bounds.Height + (Sqare.Height*2);
int Width = currentControl.Bounds.Width + (Sqare.Width*2);
//set bounds
this.Bounds = new Rectangle(X,Y,Width+1,Height+1);
this.BringToFront();
//create inside rect bounds
Rect = currentControl.Bounds;
//create transparent area around the control
this.Region = new Region(BuildFrame());
//create graphics
g = this.CreateGraphics();
}
/// <summary>
/// Transparents the control area in the tracker
/// </summary>
/// <returns>Graphics Path made for transparenting</returns>
private GraphicsPath BuildFrame()
{
//make the tracker to "contain" the control like this:
//
//
//+++++++++++++++++++++++
//+ +
//+ +
//+ CONTROL +
//+ +
//+ +
//+++++++++++++++++++++++
//
GraphicsPath path = new GraphicsPath();
//Top Rectagle
path.AddRectangle(new Rectangle(0,0,currentControl.Width + (Sqare.Width*2)+1,Sqare.Height+1));;
//Left Rectangle
path.AddRectangle(new Rectangle(0,Sqare.Height+1,Sqare.Width+1,currentControl.Bounds.Height + Sqare.Height+1));
//Bottom Rectagle
path.AddRectangle(new Rectangle(Sqare.Width+1,currentControl.Bounds.Height + Sqare.Height,currentControl.Width + Sqare.Width +1,Sqare.Height+1));
//Right Rectagle
path.AddRectangle(new Rectangle(currentControl.Width + Sqare.Width,Sqare.Height+1,Sqare.Width+1,currentControl.Height-2));
return path;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// RectTracker
//
this.BackColor = System.Drawing.Color.Wheat;
this.Name = "RectTracker";
this.Size = new System.Drawing.Size(64, 56);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.RectTracker_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.RectTracker_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.RectTracker_MouseMove);
}
#endregion
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;
}
}
private void RectTracker_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Cursor = Cursors.Arrow;
if (e.Button == MouseButtons.Left)
{
// If this is the first mouse move event for left click dragging of the form,
// store the current point clicked so that we can use it to calculate the form's
// new location in subsequent mouse move events due to left click dragging of the form
if(isFirst == true)
{
// Store previous left click position
prevLeftClick = new Point(e.X, e.Y);
// Subsequent mouse move events will not be treated as first time, until the
// left mouse click is released or other mouse click occur
isFirst = false;
}
else
{
//hide tracker
this.Visible = false;
//forward mouse position to append changes
Mouse_Move(this,e);
// Store new previous left click position
prevLeftClick = new Point(e.X, e.Y);
}
}
else
{
// This is a new mouse move event so reset flag
isFirst = true;
//show the tracker
this.Visible = true;
//update the mouse pointer to other cursors by checking it's position
Hit_Test(e.X,e.Y);
}
}
private void RectTracker_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//redraw sqares
Draw();
}
private void RectTracker_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//the mouse is up, recreate the control to append changes
Create();
this.Visible = true;
}
}
} |
Partager