AutoScaleMode.Font和动态添加的控件 [英] AutoScaleMode.Font and Dynamically added controls

查看:161
本文介绍了AutoScaleMode.Font和动态添加的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

更新

如果我将第二个NewGroup调用更改为以下形式:

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.

推荐答案

问题是, AutoScaleMode =自动缩放模式.字体 必须在所有控件都放置在表单上之后.设置AutoScaleMode后放置的所有控件都将从自动缩放中忽略.通常,设计器会将自动缩放模式的设置放入InitializeComponents()-Method中,因此,在InitializeComponents()-Method之后创建的每个控件都属于被忽略的类别.只需从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)

这篇关于AutoScaleMode.Font和动态添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆