简体   繁体   English

调整控件的大小在运行时

[英]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. 为了正常运行,你可以在其中放置一个容器和一个resizer元素,就像一个用作调整大小边框的瘦图像。 The controlToResize is the container itself. controlToResize是容器本身。 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 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! 现在button1是一个可移动且可调整大小的控件!

Try this link from CP. 从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 http://www.codeproject.com/Tips/743923/Csharp-Automatically-Resize-Controls-Runtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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