Emulate Resizing

I've got a form with No borders, but I'd like to be able to resize it. So I need to emulate the default resizing technique that Windows Forms uses. I know how to get the cursor to change to the appropriate resizing cursor, but I can't think of the logic/methods to use to actually handle resizing the form dynamically... any ideas?
LVL 2
derekthorntonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Farshid-ZakerCommented:
Suppose your form name is Form1. Add these event handlers to your form:
Load, MouseDown, MouseUp, MouseMove
Then add use the following code to modify your form code and event handlers.

Hope to be helpful,
<Farshid/>

const byte n = 4;// in fact width of your virtual border.
enum eHorizontalEdgeType {top, bottom, none};
enum eVerticalEdgeType {left, right, none};

private eHorizontalEdgeType HEdge;
private eVerticalEdgeType VEdge;

private bool bMouseHoldDown;

System.Windows.Forms.Cursor originalCursor;

private void Form1_Load(object sender, System.EventArgs e)
{
      HEdge = eHorizontalEdgeType.none;
      VEdge = eVerticalEdgeType.none;
      bMouseHoldDown = false;
      originalCursor = this.Cursor;
}

private void ChangeCursor(int x, int y)
{
      if (bMouseHoldDown)
            return;
      if (y <= n)
            if (x <= n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            else if (x >= this.Width - n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeNESW;
            else
                  this.Cursor = System.Windows.Forms.Cursors.SizeNS;
      else if (y >= this.Height - n)
            if (x <= n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeNESW;
            else if (x >= this.Width - n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            else
                  this.Cursor = System.Windows.Forms.Cursors.SizeNS;
      else if (true)//!!!
            if (x <= n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeWE;
            else if (x >= this.Width - n)
                  this.Cursor = System.Windows.Forms.Cursors.SizeWE;
            else
                  this.Cursor = originalCursor;
}

private void RestoreCursor()
{
      this.Cursor = originalCursor;
}

private bool IsOnEdge(System.Windows.Forms.MouseEventArgs e)
{
      bool onEdge = false;
      
      if (e.X <= n)
      {
            onEdge = true;
            VEdge = eVerticalEdgeType.left;
      }
      else if (e.X >= this.Width - n)
      {
            onEdge = true;
            VEdge = eVerticalEdgeType.right;
      }
      else
            VEdge = eVerticalEdgeType.none;

      if (e.Y <= n)
      {
            onEdge = true;
            HEdge = eHorizontalEdgeType.top;
      }
      else if (e.Y >= this.Height - n)
      {
            onEdge = true;
            HEdge = eHorizontalEdgeType.bottom;
      }
      else
            HEdge = eHorizontalEdgeType.none;

      return onEdge;
}

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
      if ((e.Button == System.Windows.Forms.MouseButtons.Left) && IsOnEdge(e))
            bMouseHoldDown = true;
      else
            bMouseHoldDown = false;
}

private void ResizeForm(int x, int y)
{
      if (VEdge == eVerticalEdgeType.right)
            this.Width = x;
      if (HEdge == eHorizontalEdgeType.bottom)
            this.Height = y;
      
      if (VEdge == eVerticalEdgeType.left)
      {
            this.Left += x;
            this.Width -= x;
      }
      if (HEdge == eHorizontalEdgeType.top)
      {
            this.Top += y;
            this.Height -= y;
      }
}

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
      if (bMouseHoldDown)
      {
            bMouseHoldDown = false;
            ResizeForm(e.X, e.Y);
            RestoreCursor();
      }
}

private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
      ChangeCursor(e.X, e.Y);
      if (bMouseHoldDown)
            ResizeForm(e.X, e.Y);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.