Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Emulate Resizing

Posted on 2004-10-22
1
Medium Priority
?
233 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
Farshid-Zaker earned 2000 total points
ID: 12386060
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question