Skip to content

Instantly share code, notes, and snippets.

@mamidenn
Last active June 15, 2017 19:05
Show Gist options
  • Save mamidenn/d052a6f24f73d4d2a43606193749ec17 to your computer and use it in GitHub Desktop.
Save mamidenn/d052a6f24f73d4d2a43606193749ec17 to your computer and use it in GitHub Desktop.
A simple wrapper for System.Drawing.Rectangle that allows manipulation of the Top, Right, Bottom and Left properties.
public class SelectionRectangle : IEquatable<SelectionRectangle>
{
private Rectangle rectangle;
public int Area
{
get { return rectangle.Width * rectangle.Height; }
}
public int X
{
get { return rectangle.X; }
set { rectangle.X = value; }
}
public int Y
{
get { return rectangle.Y; }
set { rectangle.Y = value; }
}
public Point Location
{
get { return rectangle.Location; }
set { rectangle.Location = value; }
}
public int Width
{
get { return rectangle.Width; }
set { rectangle.Width = value; }
}
public int Height
{
get { return rectangle.Height; }
set { rectangle.Height = value; }
}
public int Top
{
get { return rectangle.Top; }
set
{
rectangle.Height -= value - rectangle.Y;
rectangle.Y = value;
}
}
public int Right
{
get { return rectangle.Right; }
set { rectangle.Width = value - rectangle.X; }
}
public int Bottom
{
get { return rectangle.Bottom; }
set { rectangle.Height = value - rectangle.Y; }
}
public int Left
{
get { return rectangle.Left; }
set
{
rectangle.Width -= value - rectangle.X;
rectangle.X = value;
}
}
public bool IsEmpty
{
get { return rectangle.IsEmpty; }
}
public SelectionRectangle()
{
rectangle = new Rectangle();
}
public SelectionRectangle(Point location, Size size)
{
rectangle = new Rectangle(location, size);
}
public SelectionRectangle(int x, int y, int width, int height)
{
rectangle = new Rectangle(x, y, width, height);
}
public bool Contains(Point pt)
{
return rectangle.Contains(pt);
}
public bool Contains(Rectangle rect)
{
return rectangle.Contains(rect);
}
public bool Contains(int x, int y)
{
return rectangle.Contains(x, y);
}
public bool Equals(SelectionRectangle other)
{
return rectangle.Equals(other.rectangle);
}
public void Inflate(Size size)
{
rectangle.Inflate(size);
}
public void Inflate(int width, int height)
{
rectangle.Inflate(width, height);
}
public void Intersect(Rectangle rect)
{
rectangle.Intersect(rect);
}
public bool IntersectsWith(Rectangle rect)
{
return rectangle.IntersectsWith(rect);
}
public void Offset(Point pos)
{
rectangle.Offset(pos);
}
public void Offset(int x, int y)
{
rectangle.Offset(x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment