调整控件大小以保持原始比例大小Winforms C#

时间:2016-06-04 10:24:52

标签: c# winforms size controls

我试图调整我的控制权并保持他们当前的位置。我已经尝试过将原始比例保留在列表中并进行比较,并在表单大小发生变化时相应地调整大小。

结果http://prntscr.com/bc796l - > http://prntscr.com/bc79a2

你能指导我解决问题的地方吗?或者建议采用不同的方法

private void Form_Resize(object sender, EventArgs e)
    {
        Control.ControlCollection controls = Controls;
        List<Size> newSizes = new List<Size>();
        for (int j = 0; j < controls.Count; j++)
        {
            int widthProportion = Bounds.Width / controls[j].Width;
            int heightProportion = Bounds.Height/controls[j].Height;
            string controlName = controlProportions[j].Item1;

            int originalWidthProportion = controlProportions[j].Item2;
            int originalHeightProportion = controlProportions[j].Item3;
            if (controls[j].Name == controlName)
            {
                if (widthProportion > originalWidthProportion)
                {
                    while (widthProportion > originalWidthProportion)
                    {
                        originalWidthProportion++;
                    }
                }
                else if (widthProportion < originalWidthProportion)
                {
                    while (widthProportion < originalWidthProportion)
                    {
                        originalWidthProportion--;
                    }
                }
                if (heightProportion > originalHeightProportion)
                {
                    while (heightProportion > originalHeightProportion)
                    {
                        originalHeightProportion++;
                    }
                }
                else if (heightProportion < originalHeightProportion)
                {
                    while (heightProportion < originalHeightProportion)
                    {
                        originalHeightProportion--;
                    }
                }
                newSizes.Add(new Size(Bounds.Width/originalWidthProportion, Bounds.Height/originalHeightProportion));
                controlProportions[j] = new Tuple<string, int, int>(controls[j].Name, originalWidthProportion,
                    originalHeightProportion);
            }
        }
        for (int j = 0; j < controls.Count; j++)
        {
            controls[j].Size = newSizes[i];
        }
    }

这就是我最初填充列表的方式:

private static readonly List<Tuple<string,int, int>> controlProportions = new List<Tuple<string,int, int>>();

    private void SetControlProportions()
    {
        Control.ControlCollection controls = Controls;
        foreach (Control control in controls)
        {
            int heightProportion = Bounds.Height / control.Height;
            int widthProportion = Bounds.Width / control.Width;
            controlProportions.Add(new Tuple<string, int, int>(control.Name,widthProportion, heightProportion));
        }
    }

0 个答案:

没有答案