Resizing Controls At runtime

前端 未结 3 1080
一整个雨季
一整个雨季 2021-01-13 05:00

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

相关标签:
3条回答
  • 2021-01-13 05:07

    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!

    0 讨论(0)
  • 2021-01-13 05:08

    Have a look at

    • Runtime resizable controls!
    • User resizing PictureBox at runtime
    0 讨论(0)
  • 2021-01-13 05:25

    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)));
                    }
                }
            };
        }
    }
    
    0 讨论(0)
提交回复
热议问题