【问题标题】:How to automatically scale child controls' font sizes when form size changes?表单大小更改时如何自动缩放子控件的字体大小?
【发布时间】:2013-01-14 23:34:33
【问题描述】:

我正在使用 TableLayoutPanel 来包含我希望在调整表单大小时自动调整大小的控件。我想知道当表单本身被调整大小时,如何使表单的子控件“缩放”的大小与表单成比例?虽然 TableLayoutPanel 会自动调整所包含控件的大小,但这些控件保持相同的字体大小。

【问题讨论】:

  • 你见过一个程序在窗口变小时让字体变小吗?这不是它的完成方式。
  • 我发布的答案可以满足我的要求。

标签: c# .net tablelayoutpanel


【解决方案1】:

这是迄今为止我想出的最好的方法。我使用两个比例因子,并遍历所有控件以选择性地选择要缩放的那些:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestTableLayoutPanel
{
    public partial class Form2 : Form
    {
        private const float LARGER_FONT_FACTOR = 1.5f;
        private const float SMALLER_FONT_FACTOR = 0.8f;

        private int _lastFormSize;

        public Form2()
        {
            InitializeComponent();

            this.Resize += new EventHandler(Form2_Resize);
            _lastFormSize = GetFormArea(this.Size);
        }

        private int GetFormArea(Size size)
        {
            return size.Height * size.Width;
        }

        private void Form2_Resize(object sender, EventArgs e)
        {

            var bigger = GetFormArea(this.Size) > _lastFormSize;
            float scaleFactor = bigger ? LARGER_FONT_FACTOR : SMALLER_FONT_FACTOR;

            ResizeFont(this.Controls, scaleFactor);

            _lastFormSize = GetFormArea(this.Size);

        }

        private void ResizeFont(Control.ControlCollection coll, float scaleFactor)
        {
            foreach (Control c in coll)
            {
                if (c.HasChildren)
                {
                    ResizeFont(c.Controls, scaleFactor);
                }
                else
                {
                    //if (c.GetType().ToString() == "System.Windows.Form.Label")
                    if (true)
                    {
                        // scale font
                        c.Font = new Font(c.Font.FontFamily.Name, c.Font.Size * scaleFactor);
                    }
                }
            }
        }
    }
}

【讨论】:

  • 感谢 Steven P 在这里通过他的建议帮助我制定了我的问题的答案:stackoverflow.com/questions/9510577/…
  • 我编辑了您的帖子以修复代码格式(它被分成两部分)。
  • 谢谢!我不确定它是如何分裂的,但至少现在已经修复了。
  • 试过这个,注意它在使用最小化/最大化窗口按钮时效果很好,但是如果通过拖动框架边缘来调整窗口大小,字体缩放(因为没有更好的词) 爆炸
  • 测试这个样本并显示标签不合适并且标签很大。在表单和标签之间不同步大小
【解决方案2】:

根据之前的回答,我写了一个真正的字体缩放解决方案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineControlsTest
{
    public partial class Form2 : Form
    {

        private int _lastFormSize;

        public Form2()
        {
            InitializeComponent();

            this.Resize += new EventHandler(Form2_Resize);
            _lastFormSize = GetFormArea(this.Size);
        }

        private int GetFormArea(Size size)
        {
            return size.Height * size.Width;
        }

        private void Form2_Resize(object sender, EventArgs e)
        {
            Control control = (Control)sender;

            float scaleFactor = (float)GetFormArea(control.Size) / (float)_lastFormSize;

            ResizeFont(this.Controls, scaleFactor);

            _lastFormSize = GetFormArea(control.Size);

        }

        private void ResizeFont(Control.ControlCollection coll, float scaleFactor)
        {
            foreach (Control c in coll)
            {
                if (c.HasChildren)
                {
                    ResizeFont(c.Controls, scaleFactor);
                }
                else
                {
                    //if (c.GetType().ToString() == "System.Windows.Form.Label")
                    if (true)
                    {
                        // scale font
                        c.Font = new Font(c.Font.FontFamily.Name, c.Font.Size * scaleFactor);
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多