How can user resize control at runtime in winforms

Say i have a pictureBox.

Now what i want is that user should be able to resize the pictureBox at will. However i have no idea on how to even start on this thing. I have searched internet however information is scarce.

Can anybody at least guide me on where to start ?


This is pretty easy to do, every window in Windows has the innate ability to be resizable. It is just turned off for a PictureBox, you can turn it back on by listening for the WM_NCHITTEST message. You simply tell Windows that the cursor is on a corner of a window, you get everything else for free. You'll also want to draw a grab handle so it is clear to the user that dragging the corner will resize the box.

Add a new class to your project and paste the code shown below. Build + Build. You'll get a new control on top of the toolbox, drop it on a form. Set the Image property and you're set to try it.

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

class SizeablePictureBox : PictureBox {
    public SizeablePictureBox() {
        this.ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc); 
    }
    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        if (m.Msg == 0x84) {  // Trap WM_NCHITTEST
            var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
            if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
                m.Result = new IntPtr(17);  // HT_BOTTOMRIGHT
        }
    }
    private const int grab = 16;
}

Another very cheap way to get the resizing for free is by giving the control a resizable border. Which works on all corners and edges. Paste this code into the class (you don't need WndProc anymore):

protected override CreateParams CreateParams {
    get {
        var cp = base.CreateParams;
        cp.Style |= 0x840000;  // Turn on WS_BORDER + WS_THICKFRAME
        return cp;
    }
}

here is an article

http://www.codeproject.com/Articles/20716/Allow-the-User-to-Resize-Controls-at-Runtime

that should help you since it in vb here a C# translation

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{


    ResizeableControl rc;

    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        rc = new ResizeableControl(pbDemo);

    }
    public Form1()
    {
        Load += Form1_Load;
    }

}

AND RE-SIZE FUNCTION

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class ResizeableControl
{

    private Control withEventsField_mControl;
    private Control mControl {
        get { return withEventsField_mControl; }
        set {
            if (withEventsField_mControl != null) {
                withEventsField_mControl.MouseDown -= mControl_MouseDown;
                withEventsField_mControl.MouseUp -= mControl_MouseUp;
                withEventsField_mControl.MouseMove -= mControl_MouseMove;
                withEventsField_mControl.MouseLeave -= mControl_MouseLeave;
            }
            withEventsField_mControl = value;
            if (withEventsField_mControl != null) {
                withEventsField_mControl.MouseDown += mControl_MouseDown;
                withEventsField_mControl.MouseUp += mControl_MouseUp;
                withEventsField_mControl.MouseMove += mControl_MouseMove;
                withEventsField_mControl.MouseLeave += mControl_MouseLeave;
            }
        }
    }
    private bool mMouseDown = false;
    private EdgeEnum mEdge = EdgeEnum.None;
    private int mWidth = 4;

    private bool mOutlineDrawn = false;
    private enum EdgeEnum
    {
        None,
        Right,
        Left,
        Top,
        Bottom,
        TopLeft
    }

    public ResizeableControl(Control Control)
    {
        mControl = Control;
    }


    private void mControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left) {
            mMouseDown = true;
        }
    }


    private void mControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        mMouseDown = false;
    }


    private void mControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Control c = (Control)sender;
        Graphics g = c.CreateGraphics;
        switch (mEdge) {
            case EdgeEnum.TopLeft:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth * 4, mWidth * 4);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Left:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth, c.Height);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Right:
                g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, 0, c.Width, c.Height);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Top:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, c.Width, mWidth);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Bottom:
                g.FillRectangle(Brushes.Fuchsia, 0, c.Height - mWidth, c.Width, mWidth);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.None:
                if (mOutlineDrawn) {
                    c.Refresh();
                    mOutlineDrawn = false;
                }
                break;
        }

        if (mMouseDown & mEdge != EdgeEnum.None) {
            c.SuspendLayout();
            switch (mEdge) {
                case EdgeEnum.TopLeft:
                    c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height);
                    break;
                case EdgeEnum.Left:
                    c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
                    break;
                case EdgeEnum.Right:
                    c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
                    break;
                case EdgeEnum.Top:
                    c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
                    break;
                case EdgeEnum.Bottom:
                    c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
                    break;
            }
            c.ResumeLayout();
        } else {
            switch (true) {
                case e.X <= (mWidth * 4) & e.Y <= (mWidth * 4):
                    //top left corner
                    c.Cursor = Cursors.SizeAll;
                    mEdge = EdgeEnum.TopLeft;
                    break;
                case e.X <= mWidth:
                    //left edge
                    c.Cursor = Cursors.VSplit;
                    mEdge = EdgeEnum.Left;
                    break;
                case e.X > c.Width - (mWidth + 1):
                    //right edge
                    c.Cursor = Cursors.VSplit;
                    mEdge = EdgeEnum.Right;
                    break;
                case e.Y <= mWidth:
                    //top edge
                    c.Cursor = Cursors.HSplit;
                    mEdge = EdgeEnum.Top;
                    break;
                case e.Y > c.Height - (mWidth + 1):
                    //bottom edge
                    c.Cursor = Cursors.HSplit;
                    mEdge = EdgeEnum.Bottom;
                    break;
                default:
                    //no edge
                    c.Cursor = Cursors.Default;
                    mEdge = EdgeEnum.None;
                    break;
            }
        }
    }


    private void mControl_MouseLeave(object sender, System.EventArgs e)
    {
        Control c = (Control)sender;
        mEdge = EdgeEnum.None;
        c.Refresh();
    }

}

with use ControlMoverOrResizer class in this article you can do movable and resizable controls in run time just with a line of code! :) example:

ControlMoverOrResizer.Init(button1);

and now button1 is a movable and resizable control!