Solved

Emulate Resizing

Posted on 2004-10-22
226 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
Question by:derekthornton
    1 Comment
     
    LVL 4

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now