class Selection
{
public struct SelectionRecs
{
public Rectangle topLeft;
public Rectangle topRight;
public Rectangle bottomLeft;
public Rectangle bottomRight;
public Rectangle top;
public Rectangle bottom;
public Rectangle left;
public Rectangle right;
}
public Selection(Form form)
{
this.frm = form;
}
public Rectangle _rec = new Rectangle();
private Form frm = null;
SelectionRecs selRecs = new SelectionRecs();
public Point pone = new Point();
public Point ptwo = new Point();
Point ptempone = new Point();
Point ptemptwo = new Point();
Point startingPoint = new Point();
Pen borderPen = new Pen(Brushes.Black, (float).00001 );
bool mDown = false;
bool moveAll = false;
bool moveTL = false;
bool moveTR = false;
bool moveBL = false;
bool moveBR = false;
bool moveTop = false;
bool moveBottom = false;
bool moveLeft = false;
bool moveRight = false;
RectangleF bounds = new RectangleF();
public Rectangle SelectionRectangle
{
get
{
return this._rec;
}
set
{
this._rec = value;
}
}
public void DrawSelection(Graphics graphics)
{
bounds = graphics.ClipBounds;
if (pone != Point.Empty && ptwo != Point.Empty)
{
Rectangle rec = new Rectangle(pone.X, pone.Y, ptwo.X - pone.X, ptwo.Y - pone.Y);
this._rec = rec;
graphics.DrawRectangle(borderPen, rec);
drawSelectionShadow(graphics);
drawSelectionRecs(graphics);
}
else if (mDown)
{
Point cpo = new Point(ptempone.X, ptempone.Y);
Point cpt = new Point(ptemptwo.X, ptemptwo.Y);
Rectangle rec = new Rectangle(cpo.X, cpo.Y, cpt.X - cpo.X, cpt.Y - cpo.Y);
graphics.DrawRectangle(borderPen, rec);
}
}
Point lastMouse = new Point();
public void mouseMoved(System.Windows.Forms.MouseEventArgs mouse)
{
if (mDown)
{
if (mouse.Location.X < startingPoint.X && mouse.Location.Y < startingPoint.Y)
{
ptempone = mouse.Location;
if (mouse.Location.X < 0)
{
ptempone.X = 1;
}
if (mouse.Location.Y < 0)
{
ptempone.Y = 1;
}
}
else if (mouse.Location.X < startingPoint.X && mouse.Location.Y > startingPoint.Y)
{
ptempone.X = mouse.Location.X;
ptemptwo.Y = mouse.Location.Y;
if (mouse.Location.X < 0)
{
ptempone.X = 1;
}
if (mouse.Location.Y < 0)
{
ptempone.Y = 1;
}
}
else if (mouse.Location.X > startingPoint.X && mouse.Location.Y < startingPoint.Y)
{
ptempone.Y = mouse.Location.Y;
ptemptwo.X = mouse.Location.X;
if (mouse.Location.X < 0)
{
ptempone.X = 1;
}
if (mouse.Location.Y < 0)
{
ptempone.Y = 1;
}
}
else
{
ptemptwo = mouse.Location;
if (mouse.Location.X > bounds.Width)
ptemptwo.X = (int)bounds.Width;
if (mouse.Location.Y > bounds.Height)
ptemptwo.Y = (int)bounds.Height;
if (mouse.Location.X < 0)
{
ptempone.X = 1;
}
if (mouse.Location.Y < 0)
{
ptempone.Y = 1;
}
}
if (mouse.Location.X > bounds.Width)
ptemptwo.X = (int)bounds.Width;
if (mouse.Location.Y > bounds.Height)
ptemptwo.Y = (int)bounds.Height;
if (mouse.Location.X < 0)
{
ptempone.X = 1;
}
if (mouse.Location.Y < 0)
{
ptempone.Y = 1;
}
}
else if (moveAll)
{
if (bounds.Contains(mouse.Location) || bounds.Contains(pone))
{
Point uipone = new Point(mouse.Location.X - ptempone.X, mouse.Location.Y - ptempone.Y);
Point uiptwo = new Point(mouse.Location.X + ptemptwo.X, mouse.Location.Y + ptemptwo.Y);
if (bounds.Contains(uipone) && bounds.Contains(uiptwo))
{
pone = uipone;
ptwo = uiptwo;
}
else
{
if (uipone.X < 0)
{
uipone.X = 1;
uiptwo.X = _rec.Width + 1;
}
if (uipone.Y < 0)
{
uipone.Y = 1;
uiptwo.Y = _rec.Height + 1;
}
if (uiptwo.Y > (int)bounds.Height)
{
uiptwo.Y = (int)bounds.Height;
uipone.Y = (int)(bounds.Height - _rec.Height);
}
if (uiptwo.X > (int)bounds.Width)
{
uiptwo.X = (int)bounds.Width;
uipone.X = (int)(bounds.Width - _rec.Width);
}
pone = uipone;
ptwo = uiptwo;
}
}
else
{
Point uipone = new Point(mouse.Location.X - ptempone.X, mouse.Location.Y + ptempone.Y);
Point uiptwo = new Point(mouse.Location.X + ptemptwo.X, mouse.Location.Y - ptemptwo.Y);
if (mouse.Location.Y < 0)
{
uipone.Y = 1;
uiptwo.Y = _rec.Height;
}
if (mouse.Location.Y > bounds.Height)
{
uipone.Y = (int)(bounds.Height - _rec.Height);
uiptwo.Y = (int)bounds.Height;
}
if (mouse.Location.X < 0)
{
uipone.X = 1;
uiptwo.X = _rec.Width;
}
if (mouse.Location.X > bounds.Width)
{
uipone.X = (int)(bounds.Width - _rec.Width);
uiptwo.X = (int)bounds.Width;
}
pone = uipone;
ptwo = uiptwo;
}
}
else if (moveTL)
{
if (mouse.Location.X < ptwo.X - 20)
{
pone.X = mouse.Location.X;
}
if (mouse.Location.Y < ptwo.Y - 20)
{
pone.Y = mouse.Location.Y;
}
if (mouse.Location.X < 0)
{
pone.X = 1;
}
if (mouse.Location.Y < 0)
{
pone.Y = 1;
}
}
else if (moveTR)
{
if (mouse.Location.X > pone.X + 20)
{
ptwo.X = mouse.Location.X;
}
if (mouse.Location.Y < ptwo.Y - 20)
{
pone.Y = mouse.Location.Y;
}
if (mouse.Location.Y < 0)
{
pone.Y = 1;
}
if (mouse.Location.X > (int)bounds.Width)
{
ptwo.X = (int)bounds.Width;
}
}
else if (moveBL)
{
if (mouse.Location.X < ptwo.X - 20)
{
pone.X = mouse.Location.X;
}
if (mouse.Location.Y > pone.Y + 20)
{
ptwo.Y = mouse.Location.Y;
}
if (mouse.Location.Y > bounds.Height)
{
ptwo.Y = (int)bounds.Height;
}
if (mouse.Location.X < 0)
{
pone.X = 1;
}
}
else if (moveBR)
{
if (mouse.Location.X > pone.X + 20)
{
ptwo.X = mouse.Location.X;
}
if (mouse.Location.Y > pone.Y + 20)
{
ptwo.Y = mouse.Location.Y;
}
if (mouse.Location.X > bounds.Width)
{
ptwo.X = (int)bounds.Width;
}
if (mouse.Location.Y > bounds.Height)
{
ptwo.Y = (int)bounds.Height;
}
}
else if (moveTop)
{
if (mouse.Location.Y < ptwo.Y - 20)
{
pone.Y = mouse.Location.Y;
}
if (mouse.Location.Y < 0)
{
pone.Y = 1;
}
}
else if (moveBottom)
{
if (mouse.Location.Y > pone.Y + 20)
{
ptwo.Y = mouse.Location.Y;
}
if (mouse.Location.Y > bounds.Height)
{
ptwo.Y = (int)bounds.Height;
}
}
else if (moveLeft)
{
if (mouse.Location.X < ptwo.X - 20)
{
pone.X = mouse.Location.X;
}
if (mouse.Location.X < 0)
{
pone.X = 1;
}
}
else if (moveRight)
{
if (mouse.Location.X > pone.X + 20)
{
ptwo.X = mouse.Location.X;
}
if (mouse.Location.X > bounds.Width)
{
ptwo.X = (int)bounds.Width;
}
}
else
{
if (selRecs.top.Contains(mouse.Location) || selRecs.bottom.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeNS;
}
else if (selRecs.right.Contains(mouse.Location) || selRecs.left.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeWE;
}
else if (selRecs.topLeft.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeNWSE;
}
else if (selRecs.topRight.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeNESW;
}
else if (selRecs.bottomLeft.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeNESW;
}
else if (selRecs.bottomRight.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeNWSE;
}
else if (_rec.Contains(mouse.Location))
{
frm.Cursor = Cursors.SizeAll;
}
else
{
frm.Cursor = Cursors.Default;
}
}
lastMouse = mouse.Location;
}
public void mouseDown(System.Windows.Forms.MouseEventArgs mouse)
{
if (frm.Cursor == Cursors.Default)
{
mDown = true;
startingPoint = mouse.Location;
pone = mouse.Location;
ptempone = mouse.Location;
ptemptwo = mouse.Location;
ptwo = Point.Empty;
}
else
{
if (frm.Cursor == Cursors.SizeAll)
{
moveAll = true;
startingPoint = mouse.Location;
ptempone = new Point(mouse.Location.X - pone.X, mouse.Location.Y - pone.Y);
ptemptwo = new Point(ptwo.X - mouse.Location.X, ptwo.Y - mouse.Location.Y);
}
else if (frm.Cursor == Cursors.SizeNESW)
{
if (selRecs.topRight.Contains(mouse.Location))
{
moveTR = true;
}
else if (selRecs.bottomLeft.Contains(mouse.Location))
{
moveBL = true;
}
}
else if (frm.Cursor == Cursors.SizeNWSE)
{
if (selRecs.topLeft.Contains(mouse.Location))
{
moveTL = true;
}
else if (selRecs.bottomRight.Contains(mouse.Location))
{
moveBR = true;
}
}
else if (frm.Cursor == Cursors.SizeWE)
{
if (selRecs.left.Contains(mouse.Location))
{
moveLeft = true;
}
else
{
moveRight = true;
}
}
else if (frm.Cursor == Cursors.SizeNS)
{
if (selRecs.top.Contains(mouse.Location))
{
moveTop = true;
}
else
{
moveBottom = true;
}
}
}
}
public void mouseUp(System.Windows.Forms.MouseEventArgs mouse)
{
if (mDown)
{
mDown = false;
pone = ptempone;
ptwo = ptemptwo;
if (ptwo.X - pone.X < 20 && ptwo.Y - pone.Y < 20)
{
pone = Point.Empty;
ptwo = Point.Empty;
_rec = Rectangle.Empty;
ptempone = Point.Empty;
ptemptwo = Point.Empty;
selRecs.topLeft = selRecs.top = selRecs.topRight = selRecs.left = selRecs.bottomLeft = selRecs.bottom = selRecs.bottomRight = selRecs.right = Rectangle.Empty;
}
ptemptwo = Point.Empty;
ptempone = Point.Empty;
}
else
{
if (moveAll)
{
moveAll = false;
}
else if (moveTL)
{
moveTL = false;
}
else if (moveTR)
{
moveTR = false;
}
else if (moveBL)
{
moveBL = false;
}
else if (moveBR)
{
moveBR = false;
}
else if (moveTop)
{
moveTop = false;
}
else if (moveBottom)
{
moveBottom = false;
}
else if (moveLeft)
{
moveLeft = false;
}
else if (moveRight)
{
moveRight = false;
}
}
}
public void correctPoints(Point one, Point two)
{
if (one.X > two.X)
{
int tmpx = one.X;
one.X = two.X;
two.X = tmpx;
}
if (one.Y > two.Y)
{
int tmpy = one.Y;
one.Y = two.Y;
two.Y = tmpy;
}
}
public void drawSelectionRecs(Graphics graph)
{
selRecs.topLeft = new Rectangle(pone.X - 4, pone.Y - 4, 8, 8);
selRecs.topRight = new Rectangle(ptwo.X - 4, pone.Y - 4, 8, 8);
selRecs.bottomLeft = new Rectangle(pone.X - 4, ptwo.Y - 4, 8, 8);
selRecs.bottomRight = new Rectangle(ptwo.X - 4, ptwo.Y - 4, 8, 8);
selRecs.top = new Rectangle(((ptwo.X - pone.X) / 2) + pone.X - 3, pone.Y - 3, 6, 6);
selRecs.bottom = new Rectangle(((ptwo.X - pone.X) / 2) + pone.X - 3, ptwo.Y - 3, 6, 6);
selRecs.left = new Rectangle(pone.X - 3, (((ptwo.Y - pone.Y) / 2) + pone.Y) - 3, 6, 6);
selRecs.right = new Rectangle(ptwo.X - 3, (((ptwo.Y - pone.Y) / 2) + pone.Y) - 3, 6, 6);
graph.DrawRectangle(borderPen, selRecs.topLeft);
graph.DrawRectangle(borderPen, selRecs.topRight);
graph.DrawRectangle(borderPen, selRecs.bottomLeft);
graph.DrawRectangle(borderPen, selRecs.bottomRight);
graph.DrawRectangle(borderPen, selRecs.top);
graph.DrawRectangle(borderPen, selRecs.bottom);
graph.DrawRectangle(borderPen, selRecs.left);
graph.DrawRectangle(borderPen, selRecs.right);
}
public void drawSelectionShadow(Graphics graph)
{
Color transColor = Color.FromArgb(32, 0, 0, 0);
Brush brsh = new SolidBrush(transColor);
graph.FillRectangle(brsh, new Rectangle(pone.X, 0, (int)graph.ClipBounds.Width * 2, pone.Y));
graph.FillRectangle(brsh, new Rectangle(0, 0, pone.X, (int)graph.ClipBounds.Height));
graph.FillRectangle(brsh, new Rectangle(ptwo.X, pone.Y, (int)graph.ClipBounds.Width - ptwo.X, (int)graph.ClipBounds.Height - pone.Y));
graph.FillRectangle(brsh, new Rectangle(pone.X, ptwo.Y, ptwo.X - pone.X, (int)graph.ClipBounds.Height - ptwo.Y));
}
|