context: using System;
  using System.ComponentModel;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Windows.Forms;
  
  namespace Creek.UI
  {
      public class ResizeControl : IDisposable
      {
          #region "Private Controls"
  
          private readonly ResizeBox bottomBox;
          private readonly ResizeBox bottomLeftBox;
          private readonly ResizeBox bottomRightBox;
          private readonly ResizeBox leftBox;
          private readonly ResizeBox rightBox;
          private readonly ResizeBox topBox;
          private readonly ResizeBox topLeftBox;
          private readonly ResizeBox topRightBox;
  
          private class ResizeBox : UserControl
          {
              #region BoxPosition enum
  
              public enum BoxPosition
              {
                  Top,
                  Bottom,
                  Left,
                  Right,
                  TopLeft,
                  TopRight,
                  BottomLeft,
                  BottomRight
              }
  
              #endregion
  
              private readonly IContainer components = null;
  
              private BoxPosition _Position = BoxPosition.Top;
  
              public ResizeBox(BoxPosition position)
              {
                  InitializeComponent();
                  Position = position;
              }
  
              public BoxPosition Position
              {
                  get { return _Position; }
                  set { _Position = value; }
              }
  
              protected override void Dispose(bool disposing)
              {
                  if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
  
              private void InitializeComponent()
              {
                  SuspendLayout();
                  AutoScaleDimensions = new SizeF(6F, 13F);
                  AutoScaleMode = AutoScaleMode.Font;
                  BackColor = Color.White;
                  BorderStyle = BorderStyle.FixedSingle;
                  Name = "ResizeBox";
                  Size = new Size(6, 6);
                  MouseEnter += ResizeBox_MouseEnter;
                  MouseLeave += ResizeBox_MouseLeave;
                  ResumeLayout(false);
              }
  
              private void ResizeBox_MouseEnter(object sender, EventArgs e)
              {
                  switch (Position)
                  {
                      case BoxPosition.Top:
                          Cursor = Cursors.SizeNS;
                          break;
                      case BoxPosition.Bottom:
                          Cursor = Cursors.SizeNS;
                          break;
                      case BoxPosition.Left:
                          Cursor = Cursors.SizeWE;
                          break;
                      case BoxPosition.Right:
                          Cursor = Cursors.SizeWE;
                          break;
                      case BoxPosition.TopLeft:
                          Cursor = Cursors.SizeNWSE;
                          break;
                      case BoxPosition.BottomRight:
                          Cursor = Cursors.SizeNWSE;
                          break;
                      case BoxPosition.TopRight:
                          Cursor = Cursors.SizeNESW;
                          break;
                      case BoxPosition.BottomLeft:
                          Cursor = Cursors.SizeNESW;
                          break;
                      default:
                          Cursor = Cursors.No;
                          break;
                  }
              }
  
              private void ResizeBox_MouseLeave(object sender, EventArgs e)
              {
                  Cursor = Cursors.Default;
              }
          }
  
          #endregion
  
          #region "Public Methods"
  
          public ResizeControl(Control target, Boolean showResizeBoxes)
          {
              _Target = target;
  
              target.Parent.Paint += Parent_Paint;
  
              topBox = new ResizeBox(ResizeBox.BoxPosition.Top);
              bottomBox = new ResizeBox(ResizeBox.BoxPosition.Bottom);
              leftBox = new ResizeBox(ResizeBox.BoxPosition.Left);
              rightBox = new ResizeBox(ResizeBox.BoxPosition.Right);
              topLeftBox = new ResizeBox(ResizeBox.BoxPosition.TopLeft);
              topRightBox = new ResizeBox(ResizeBox.BoxPosition.TopRight);
              bottomLeftBox = new ResizeBox(ResizeBox.BoxPosition.BottomLeft);
              bottomRightBox = new ResizeBox(ResizeBox.BoxPosition.BottomRight);
  
              topLeftBox.MouseDown += Boxes_MouseDown;
              topBox.MouseDown += Boxes_MouseDown;
              topRightBox.MouseDown += Boxes_MouseDown;
              leftBox.MouseDown += Boxes_MouseDown;
              rightBox.MouseDown += Boxes_MouseDown;
              bottomLeftBox.MouseDown += Boxes_MouseDown;
              bottomBox.MouseDown += Boxes_MouseDown;
              bottomRightBox.MouseDown += Boxes_MouseDown;
  
              topLeftBox.MouseMove += topLeftBox_MouseMove;
              topBox.MouseMove += topBox_MouseMove;
              topRightBox.MouseMove += topRightBox_MouseMove;
              leftBox.MouseMove += leftBox_MouseMove;
              rightBox.MouseMove += rightBox_MouseMove;
              bottomLeftBox.MouseMove += bottomLeftBox_MouseMove;
              bottomBox.MouseMove += bottomBox_MouseMove;
              bottomRightBox.MouseMove += bottomRightBox_MouseMove;
  
              if (showResizeBoxes)
                  ShowResizeBoxes();
          }
  
          void IDisposable.Dispose()
          {
              HideResizeBoxes();
          }
  
          public void Parent_Paint(object sender, PaintEventArgs e)
          {
              Graphics g = e.Graphics;
              var pen = new Pen(Brushes.Black, 1);
              pen.DashStyle = DashStyle.Dot;
              g.DrawRectangle(pen, new Rectangle(Target.Left - 3, Target.Top - 3, Target.Width + 6, Target.Height + 6));
          }
  
          public void ShowResizeBoxes()
          {
              PositionTopLeftBox();
              PositionTopBox();
              PositionTopRightBox();
              PositionLeftBox();
              PositionRightBox();
              PositionBottomLeftBox();
              PositionBottomBox();
              PositionBottomRightBox();
              Target.Parent.Controls.Add(topBox);
              Target.Parent.Controls.Add(bottomBox);
              Target.Parent.Controls.Add(leftBox);
              Target.Parent.Controls.Add(rightBox);
              Target.Parent.Controls.Add(topLeftBox);
              Target.Parent.Controls.Add(topRightBox);
              Target.Parent.Controls.Add(bottomLeftBox);
              Target.Parent.Controls.Add(bottomRightBox);
          }
  
          public void HideResizeBoxes()
          {
          }
  
          #endregion
  
          #region "Move Event Handlers"
  
          private Point mouseLocation;
  
          private void Boxes_MouseDown(object sender, MouseEventArgs e)
          {
              mouseLocation.X = e.X;
              mouseLocation.Y = e.Y;
          }
  
          private void topLeftBox_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  Int32 newBoxTop = topLeftBox.Top + (e.Y - mouseLocation.Y);
                  Int32 oldTargetTop = Target.Top;
                  Int32 newTargetHeight = Target.Height + (Target.Top - (topLeftBox.Top + topLeftBox.Height + 1));
                  Int32 newBoxLeft = topLeftBox.Left + (e.X - mouseLocation.X);
                  Int32 oldTargetLeft = Target.Left;
                  Int32 newTargetWidth = Target.Width + (oldTargetLeft - Target.Left);
  
                  if (newTargetWidth > 30 || newBoxLeft <= topLeftBox.Left)
                  {
                      Target.Left = newBoxLeft + topLeftBox.Width + 1;
                      Target.Width += (oldTargetLeft - Target.Left);
                      topLeftBox.Left = newBoxLeft;
                      PositionTopLeftBox();
                      PositionBottomLeftBox();
                      PositionTopBox();
                      PositionBottomBox();
                      PositionLeftBox();
                  }
                  if (newTargetHeight > 15 || newBoxTop <= topBox.Top)
                  {
                      Target.Top = newBoxTop + topLeftBox.Height + 1;
                      Target.Height += (oldTargetTop - Target.Top);
                      topLeftBox.Top = newBoxTop;
                      PositionTopLeftBox();
                      PositionTopRightBox();
                      PositionLeftBox();
                      PositionRightBox();
                      PositionTopBox();
                  }
                  Target.Parent.Invalidate(new Rectangle(Target.Left - 6, Target.Top - 6, Target.Width + 12,
                                                         Target.Height + 12));
              }
          }
  
          private void topBox_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  Int32 newBoxTop = topBox.Top + (e.Y - mouseLocation.Y);
                  Int32 oldTargetTop = Target.Top;
                  Int32 newTargetHeight = Target.Height + (Target.Top - (topBox.Top + topBox.Height + 1));
  
                  if (newTargetHeight > 15 || newBoxTop <= topBox.Top)
                  {
                      Target.Top = newBoxTop + topBox.Height + 1;
                      Target.Height += (oldTargetTop - Target.Top);
                      topBox.Top = newBoxTop;
                      PositionTopLeftBox();
                      PositionTopRightBox();
                      PositionLeftBox();
                      PositionRightBox();
                  }
                  Target.Parent.Invalidate(new Rectangle(Target.Left - 6, Target.Top - 6, Target.Width + 12,
                                                         Target.Height + 12));
              }
          }
  
          private void topRightBox_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  Int32 newBoxTop = topRightBox.Top + (e.Y - mouseLocation.Y);
                  Int32 oldTargetTop = Target.Top;
                  Int32 newTargetHeight = Target.Height + (Target.Top - (topRightBox.Top + topRightBox.Height + 1));
                  Int32 newBoxLeft = topRightBox.Left + (e.X - mouseLocation.X);
                  Int32 newTargetWidth = topRightBox.Left - Target.Left - 1;
  
  
gt:                 if (newTargetWidth > 30 || newBoxLeft >= topRightBox.Left)