AutoScaleMode.Font 和动态添加的控件

发布于 2024-08-22 14:16:11 字数 2063 浏览 3 评论 0原文

我在使用表单字体大小正确缩放应用程序中的控件时遇到了一些问题。问题在于表单动态添加控件以响应用户操作。当字体大小最初设置为完美时,窗体上的任何控件都可以完美缩放,但后来添加的控件就会出现问题。它们的字体可以正确缩放,但它们的位置和大小却不能。要查看此操作的实际效果,请创建一个带有空表单的简单项目,然后粘贴以下代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        SplitContainer split = new SplitContainer();
        split.Dock = DockStyle.Fill;
        this.Controls.Add(split);

        // Group 1
        split.Panel1.Controls.Add(NewGroup());

        this.Font = new Font(this.Font.FontFamily, this.Font.Size * 2);

        // Group 2
        split.Panel2.Controls.Add(NewGroup());

        split.SplitterDistance = this.Width / 2;
    }

    public GroupBox NewGroup()
    {
        GroupBox groupBox = new GroupBox();
        groupBox.Size = new System.Drawing.Size(132, 92);
        groupBox.Text = "groupBox";
        groupBox.SuspendLayout();

        Label label = new Label();
        label.AutoSize = true;
        label.Location = new Point(6, 16);
        label.Text = "label";
        groupBox.Controls.Add(label);

        Button button = new Button();
        button.Location = new Point(6, 58);
        button.Size = new Size(93, 28);
        button.Text = "button";
        groupBox.Controls.Add(button);

        CheckBox checkBox = new CheckBox();
        checkBox.AutoSize = true;
        checkBox.Location = new Point(47, 16);
        checkBox.Text = "checkBox";
        groupBox.Controls.Add(checkBox);

        TextBox textBox = new TextBox();
        textBox.Location = new Point(6, 34);
        textBox.Size = new Size(120, 20);
        textBox.Text = "text";
        groupBox.Controls.Add(textBox);

        groupBox.ResumeLayout();

        return groupBox;
    }
}

您可以在添加的第二个组框中看到我正在讨论的效果。如何在初始大小更改后添加控件以正确缩放?

更新

如果我将第二个 NewGroup 调用更改为如下所示:

        GroupBox group = NewGroup();
        split.Panel2.Controls.Add(group);
        group.Scale(new SizeF(2.0f, 2.0f));

结果几乎是正确的。在很多情况下,它往往会偏离一两个像素,而在复杂的形式中,这种情况开始变得更加明显。我确实需要控件之间的缩放比例尽可能一致,所以我想避免这种方法。

I've been having some trouble scaling controls in my application properly with the form font size. The problem is that the form dynamically adds controls in response to user actions. Any controls that are on the form when the font size is initially set scale perfectly, but those added afterwards have issues. Their font scales properly, but their position and size don't. To see this in action, create a simple project with an empty form and paste in the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        SplitContainer split = new SplitContainer();
        split.Dock = DockStyle.Fill;
        this.Controls.Add(split);

        // Group 1
        split.Panel1.Controls.Add(NewGroup());

        this.Font = new Font(this.Font.FontFamily, this.Font.Size * 2);

        // Group 2
        split.Panel2.Controls.Add(NewGroup());

        split.SplitterDistance = this.Width / 2;
    }

    public GroupBox NewGroup()
    {
        GroupBox groupBox = new GroupBox();
        groupBox.Size = new System.Drawing.Size(132, 92);
        groupBox.Text = "groupBox";
        groupBox.SuspendLayout();

        Label label = new Label();
        label.AutoSize = true;
        label.Location = new Point(6, 16);
        label.Text = "label";
        groupBox.Controls.Add(label);

        Button button = new Button();
        button.Location = new Point(6, 58);
        button.Size = new Size(93, 28);
        button.Text = "button";
        groupBox.Controls.Add(button);

        CheckBox checkBox = new CheckBox();
        checkBox.AutoSize = true;
        checkBox.Location = new Point(47, 16);
        checkBox.Text = "checkBox";
        groupBox.Controls.Add(checkBox);

        TextBox textBox = new TextBox();
        textBox.Location = new Point(6, 34);
        textBox.Size = new Size(120, 20);
        textBox.Text = "text";
        groupBox.Controls.Add(textBox);

        groupBox.ResumeLayout();

        return groupBox;
    }
}

You can see the effect that I'm talking about in the second groupbox added. What can I do to get controls added after the initial size change to scale correctly?

UPDATE

If I change the second NewGroup call to look like this:

        GroupBox group = NewGroup();
        split.Panel2.Controls.Add(group);
        group.Scale(new SizeF(2.0f, 2.0f));

The result is ALMOST correct. It tends to be off by a pixel or two in a lot of cases, and in complex forms this starts to show up much more visibly. I really need the scaling to be as consistent as possible between controls, so I'd like to avoid this approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我ぃ本無心為│何有愛 2024-08-29 14:16:11

问题是,调用
AutoScaleMode=AutoScaleMode.Font
必须在所有控件都放置在表单上之后进行。自动缩放时将忽略设置 AutoScaleMode 后放置的所有控件。通常,设计器将自动缩放模式的设置放入InitializeComponents()方法中,因此在InitializeComponents()方法之后创建的每个控件都属于忽略类别。只需从 InitializeComponents()-Method 中删除该行并将其替换到表单构造函数的末尾即可。

(即使问题很旧,答案也可能对其他人有帮助)

The problem is, that the call to
AutoScaleMode=AutoScaleMode.Font
has to come AFTER all controls have been placed on the form. All controls you place after setting the AutoScaleMode are ignored from autoscaling. Generally, the Designer places the setting of the autoscalemode into the InitializeComponents()-Method, so every controls you create after the InitializeComponents()-Method fall into the ignored-category. Just remove the line from the InitializeComponents()-Method and replace it at the end of your forms constructor.

(even the question is old, the answer may help others)

追我者格杀勿论 2024-08-29 14:16:11

我认为你应该这样做 - 使用 Graphics.DpiX 和 Graphics.DpiY 获取当前 dpi,然后将其除以默认 dpi(通常为 96),并乘以动态添加的未出现控件的位置和大小值就这个比例。

I think that you should do this - use Graphics.DpiX and Graphics.DpiY to get current dpi, then divide it with your default dpi (usually it is 96) and multiply position and size values for your dynamically added controls that don't appear right with this ratio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文