locked
Resizing Panel Control

    Question

  • Hi,

    In my C# application, how do I allow a user to resize panels in runtime?

    Thanks.
    Friday, March 27, 2009 5:17 PM

Answers

  • Add a new class to your project and paste the code shown below.  Compile.  Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class SizeablePanel : Panel {
      private const int cGripSize = 20;
      private bool mDragging;
      private Point mDragPos;

      public SizeablePanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.BackColor = Color.White;
      }

      protected override void OnPaint(PaintEventArgs e) {
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
          new Rectangle(this.ClientSize.Width - cGripSize, this.ClientSize.Height - cGripSize, cGripSize, cGripSize));
        base.OnPaint(e);
      }

      private bool IsOnGrip(Point pos) {
        return pos.X >= this.ClientSize.Width - cGripSize &&
               pos.Y >= this.ClientSize.Height - cGripSize;
      }

      protected override void OnMouseDown(MouseEventArgs e) {
        mDragging = IsOnGrip(e.Location);
        mDragPos = e.Location;
        base.OnMouseDown(e);
      }

      protected override void OnMouseUp(MouseEventArgs e) {
        mDragging = false;
        base.OnMouseUp(e);
      }

      protected override void OnMouseMove(MouseEventArgs e) {
        if (mDragging) {
          this.Size = new Size(this.Width + e.X - mDragPos.X,
            this.Height + e.Y - mDragPos.Y);
          mDragPos = e.Location;
        }
        else if (IsOnGrip(e.Location)) this.Cursor = Cursors.SizeNWSE;
        else this.Cursor = Cursors.Default;
        base.OnMouseMove(e);
      }
    }

    Hans Passant.
    • Proposed as answer by JohnGrove Friday, March 27, 2009 8:31 PM
    • Marked as answer by obrienkev Monday, March 30, 2009 11:51 AM
    Friday, March 27, 2009 7:53 PM
    Moderator

All replies

  • Use the ControlPaint.DrawSizeGrip method to draw a sizing grip on the panel.  In  the Panel's MouseDown  event, store e.Location in  a class level variable.  In  the Panel's  MouseMove event, if the loacation is within the sizing  grip, adjust the Panel's size by comparing the location in  the MouseMove event to the stored location of the MouseDown event.
    Friday, March 27, 2009 6:56 PM
  • Add a new class to your project and paste the code shown below.  Compile.  Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class SizeablePanel : Panel {
      private const int cGripSize = 20;
      private bool mDragging;
      private Point mDragPos;

      public SizeablePanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.BackColor = Color.White;
      }

      protected override void OnPaint(PaintEventArgs e) {
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
          new Rectangle(this.ClientSize.Width - cGripSize, this.ClientSize.Height - cGripSize, cGripSize, cGripSize));
        base.OnPaint(e);
      }

      private bool IsOnGrip(Point pos) {
        return pos.X >= this.ClientSize.Width - cGripSize &&
               pos.Y >= this.ClientSize.Height - cGripSize;
      }

      protected override void OnMouseDown(MouseEventArgs e) {
        mDragging = IsOnGrip(e.Location);
        mDragPos = e.Location;
        base.OnMouseDown(e);
      }

      protected override void OnMouseUp(MouseEventArgs e) {
        mDragging = false;
        base.OnMouseUp(e);
      }

      protected override void OnMouseMove(MouseEventArgs e) {
        if (mDragging) {
          this.Size = new Size(this.Width + e.X - mDragPos.X,
            this.Height + e.Y - mDragPos.Y);
          mDragPos = e.Location;
        }
        else if (IsOnGrip(e.Location)) this.Cursor = Cursors.SizeNWSE;
        else this.Cursor = Cursors.Default;
        base.OnMouseMove(e);
      }
    }

    Hans Passant.
    • Proposed as answer by JohnGrove Friday, March 27, 2009 8:31 PM
    • Marked as answer by obrienkev Monday, March 30, 2009 11:51 AM
    Friday, March 27, 2009 7:53 PM
    Moderator
  • Absolutely brilliant nobugz
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, March 27, 2009 8:31 PM
  • Brilliant!! Thanks.
    Monday, March 30, 2009 11:52 AM
  • Add a new class to your project and paste the code shown below.  Compile.  Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class SizeablePanel : Panel {
      private const int cGripSize = 20;
      private bool mDragging;
      private Point mDragPos;

      public SizeablePanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.BackColor = Color.White;
      }

      protected override void OnPaint(PaintEventArgs e) {
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
          new Rectangle(this.ClientSize.Width - cGripSize, this.ClientSize.Height - cGripSize, cGripSize, cGripSize));
        base.OnPaint(e);
      }

      private bool IsOnGrip(Point pos) {
        return pos.X >= this.ClientSize.Width - cGripSize &&
               pos.Y >= this.ClientSize.Height - cGripSize;
      }

      protected override void OnMouseDown(MouseEventArgs e) {
        mDragging = IsOnGrip(e.Location);
        mDragPos = e.Location;
        base.OnMouseDown(e);
      }

      protected override void OnMouseUp(MouseEventArgs e) {
        mDragging = false;
        base.OnMouseUp(e);
      }

      protected override void OnMouseMove(MouseEventArgs e) {
        if (mDragging) {
          this.Size = new Size(this.Width + e.X - mDragPos.X,
            this.Height + e.Y - mDragPos.Y);
          mDragPos = e.Location;
        }
        else if (IsOnGrip(e.Location)) this.Cursor = Cursors.SizeNWSE;
        else this.Cursor = Cursors.Default;
        base.OnMouseMove(e);
      }
    }

    Hans Passant.

    I just checked it, it's great!

     

    Does anyone know of a class that is a sizeable panel but allows drawing graphics on it? This doesn't seem to allow :S

    Sunday, January 23, 2011 7:17 PM