title: Resizing a Rectangle while dragging on Windows Form
body: <p>I have a Rectangle Drawn on top of my Windows Form and I want to resize it using one of the handles provided !</p>
  
  <p><img src="https://i.stack.imgur.com/f1Zql.png" alt="enter image description here"></p>
  
  <pre><code>Rectangle areaRect = new Rectangle(100,100, 300, 300);
  Bool dragging = false;
  Point ptOld = new Point(0, 0);
  
  protected override void OnPaint(PaintEventArgs e)
  {
    Graphics dcPaint = e.Graphics;
    dcPaint.DrawRectangle(rectPen, areaRect);
  }
  
  protected override void OnMouseDown(MouseEventArgs e)
  {
    ptOld = new Point(e.X, e.Y);
    dragging = true;
  }
  
  protected override void OnMouseMove(MouseEventArgs e)
  {
    if(dragging = true) 
    {
      Point ptNew = new Point(e.X, e.Y);
      Int32 handleSelected = GetSelectedHandle(ptNew);
  
     // Lets say I want to resize this rectangle using Handle 2 now.
      if(handleSelected == 2) 
      {
        // I am resizing this rectangle Width
        areaRect.X += ptNew.X - ptOld.X;
        areaRect.Width -= ptNew .X - ptOld.X;
  
        this.Invalidate();
      }
    }
  }
  
  protected override void OnMouseUp(MouseEventArgs e)
  {
    dragging = false;
  }
  </code></pre>
  
  <p>It will give me an effect like this. Which is correct, </p>
  
  <p><img src="https://i.stack.imgur.com/POYp1.png" alt="enter image description here"></p>
  
  <p>How ever I want to have a small tweak in this, I would like to change the height of this rectangle as well, When I am moving point 2, my point 7 should remain exactly as it is, Something like this... 
  Similarly when I moving point 4, my point 5 should intact and so on for point 7 and 2 also.</p>
  
  <p><img src="https://i.stack.imgur.com/JdQjm.png" alt="enter image description here"></p>
  
  <p>Any idea, how to proceed, because if I change the height, my point 7 location also gets changed ?</p>