简体   繁体   中英

Resizing Controls At runtime

Does anybody know of any sample code laying around anywhere that would enable me to resize a picturebox at runtime when the mouse cursor is draging the bottom right edge of the control? Any help at all will be appreciated.

Thank you

You can use Use this "home made" class. For a correct functioning you shuld have a container and a resizer element inside it, like a thin image working as a resizing border. The controlToResize is the container itself. You can put all you want inside the control. Example:

ControlResizer.Init(myPictureBox, myTableLayoutPanel, ControlResizer.Direction.Vertical, Cursors.SizeNS);

Here is the class.

class ControlResizer
{
    public enum Direction
    {
        Horizontal,
        Vertical
    }

    public static void Init(Control resizer, Control controlToResize, Direction direction, Cursor cursor)
    {
        bool dragging = false;
        Point dragStart = Point.Empty;
        int maxBound;
        int minBound;

        resizer.MouseHover += delegate(object sender, EventArgs e)
        {
            resizer.Cursor = cursor;
        };

        resizer.MouseDown += delegate(object sender, MouseEventArgs e)
        {
            dragging = true;
            dragStart = new Point(e.X, e.Y);
            resizer.Capture = true;
        };

        resizer.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            dragging = false;
            resizer.Capture = false;
        };

        resizer.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                if (direction == Direction.Vertical)
                {
                    minBound = resizer.Height;
                    maxBound = controlToResize.Parent.Height - controlToResize.Top - 20;
                    controlToResize.Height = Math.Min(maxBound , Math.Max(minBound, controlToResize.Height + (e.Y - dragStart.Y)) );
                }
                if (direction == Direction.Horizontal)
                {
                    minBound = resizer.Width;
                    maxBound = controlToResize.Parent.Width - controlToResize.Left - 20;
                    controlToResize.Width = Math.Min(maxBound, Math.Max(minBound, controlToResize.Width + (e.X - dragStart.X)));
                }
            }
        };
    }
}

with use

ControlMoverOrResizer

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

ControlMoverOrResizer.Init(button1);   

and now button1 is a movable and resizable control!

Try this link from CP. You can use it as your reference. Code is for beginner I think. http://www.codeproject.com/Tips/743923/Csharp-Automatically-Resize-Controls-Runtime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM