首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改默认字体时出现的AutoScaleMode问题

更改默认字体时出现的AutoScaleMode问题
EN

Stack Overflow用户
提问于 2010-06-04 18:12:43
回答 8查看 101.8K关注 0票数 37

在使用非默认字体时,我在使用Form.AutoScaleMode属性和固定大小控件时遇到一些问题。我将其归结为一个简单的测试应用程序(WinForms 2.0),它只有一个表单、一些固定大小的控件和以下属性:

代码语言:javascript
复制
class Form1 : Form
{
    // ...
    private void InitializeComponent()
    {
        // ...
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        this.Font = new System.Drawing.Font("Tahoma", 9.25F);
        // ...
    }
}

在96dpi的Windows XP下,表单看起来如下面的96dpi示例所示:

在120 dpi的Windows XP下,Windows窗体自动缩放功能会生成此120 dpi的示例:

正如您所看到的,分组框、按钮、列表或树视图的缩放正确,多行文本框在垂直轴上变得太大,固定大小的标签在垂直和水平方向上都不能正确缩放。似乎是.NET框架中的错误?

编辑:一些提示:字体更改仅应用于包含的窗体,控件从窗体继承其字体。如果可能的话,我想让它保持这种状态。

使用默认字体(Microsoft Sans Serif 8.25pt)时,不会出现此问题。使用AutoScaleMode = Font (当然,有足够的AutoScaleDimensions )要么根本不能缩放,要么完全像上面看到的那样缩放,这取决于字体设置的时间(在AutoScaleMode更改之前或之后)。问题不是特定于"Tahoma“字体,它也发生在Microsoft Sans Serif,9.25pt。

是的,我已经读过这篇文章了,所以在high DPI problems上发表,但它对我并没有真正的帮助。

有什么建议可以解决这个问题吗?

EDIT2:关于我的意图的一些额外信息:我已经有大约50个固定大小的对话框,其中有数百个适当放置的、固定大小的控件。它们是从旧的Winforms框架迁移到C#/ C++,这就是为什么它们都是固定大小的原因。使用9.25pt字体的96dpi,所有这些看起来都很好。在旧的框架下,扩展到120dpi很好-所有固定大小的控件在两个维度上的缩放都是相同的。上周,当切换到120dpi时,我们在WinForms下检测到了这种奇怪的缩放行为。你可以想象,在120dpi下,我们的大多数对话框看起来都很糟糕。我正在寻找一种解决方案,避免完全重新设计所有这些对话框。

EDIT3:为了测试这种行为,我认为设置一个120dpi的虚拟Windows XP环境是个好主意,而开发环境驻留在96dpi以下(至少我是这么做的)。在Win XP下,在96和120 dpi之间切换通常需要重新启动,否则您看不到实际发生的情况。

代码语言:javascript
复制
// As requested: the source code of Form1.cs 
namespace DpiChangeTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Font f = this.textBox1.Font;
        }
    }
}

 // here the source of Form1.Designer.cs:
namespace DpiChangeTest
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Forms Designer generated code

        private void InitializeComponent()
        {
            System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("A list view control");
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("A TreeView control");
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.listView1 = new System.Windows.Forms.ListView();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 107);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(150, 70);
            this.button1.TabIndex = 0;
            this.button1.Text = "Just a button";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // groupBox1
            // 
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(150, 70);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Just a groupbox";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(180, 12);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(150, 70);
            this.textBox1.TabIndex = 2;
            this.textBox1.Text = "A multiline text box";
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label1.Location = new System.Drawing.Point(179, 107);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(150, 70);
            this.label1.TabIndex = 3;
            this.label1.Text = "A label with AutoSize=False";
            // 
            // listView1
            // 
            this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem2});
            this.listView1.Location = new System.Drawing.Point(12, 201);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(150, 70);
            this.listView1.TabIndex = 4;
            this.listView1.UseCompatibleStateImageBehavior = false;
            // 
            // treeView1
            // 
            this.treeView1.Location = new System.Drawing.Point(179, 201);
            this.treeView1.Name = "treeView1";
            treeNode2.Name = "Knoten0";
            treeNode2.Text = "A TreeView control";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode2});
            this.treeView1.Size = new System.Drawing.Size(150, 70);
            this.treeView1.TabIndex = 5;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.ClientSize = new System.Drawing.Size(343, 289);
            this.Controls.Add(this.treeView1);
            this.Controls.Add(this.listView1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.groupBox1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.TreeView treeView1;
    }
}

 // and Main.cs
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-06-15 14:35:13

我终于找到了我的问题的答案。简而言之,如果单独设置每个控件的字体,而不是设置包含窗体的字体,则不会产生该效果。这样,弹性伸缩功能就可以正常工作了。有趣的是,设置控件的字体会更改自动缩放行为,即使AutoScaleMode属性设置为AutoScaleMode.Dpi也是如此,而不仅仅是当它设置为AutoScaleMode.Font时。

作为一个实用的解决方案,我们创建了一个小的命令行程序,该程序读取designer.cs文件,扫描是否所有控件都有明确的字体分配,如果没有,则将该分配添加到新创建的设计器代码副本中。我们将此程序嵌入到我们的自动测试套件中,因此每当窗体获得新控件或添加新窗体时,开发人员忘记添加显式字体分配,测试就会失败。在此期间,从我第一次提出这个问题(4年前)起,我们就有了这个解决方案,从那时起,它让我们避免了几次扩展问题。

票数 39
EN

Stack Overflow用户

发布于 2015-10-17 03:50:38

然而,如果这个令人沮丧的问题导致表单上的间距变得不成比例,那么将AutoScaleMode更改为"None“,这个问题就完全消失了。

票数 3
EN

Stack Overflow用户

发布于 2016-03-16 21:48:37

请记住,由于提到的许多问题都与字体大小有关,因此与字体系列和大小保持一致是很重要的。这意味着在基本窗体或基本用户控件(如果有)上设置字体,并让控件继承该设置。我注意到,当我有一个包含UserControl的窗体时,如果我选择控件并更改字体大小,一些项会调整大小,而另一些项则不会。我意识到没有调整大小的项的字体设置是专门设置的(被覆盖)。正是在那时,我意识到当属性以粗体突出显示时意味着什么。因此,例如,如果你有一个标签,字体道具是粗体的,这意味着有人改变了它。您应该清除所有以这种方式设置的字体属性,以便它们从父级获取字体,在本例中是包含表单。您可以简单地突出显示字体属性文本并删除,或者右键单击字体属性并选择清除。这将从设计器文件中移除字体行,并允许控件从其父控件继承字体。这可能会跳回Microsoft Sans Serif,但如果你构建它将从它的父级中拾取字体,当然你应该遵循适当的设计,使用布局面板和锚点和停靠属性,但我发现,通常情况下,如果你清除所有被覆盖的字体道具,你会发现,如果你从你的窗体中选择用户控件并将autoscalemode更改为None,你也会有更好的测试机会,如果你随后改变用户控件的字体大小,那么它中的所有控件都应该重新调整大小(只要没有字体道具被覆盖),并且只要窗体是使用适当的布局面板设计的,在其他分辨率下,所有内容都应该显示正常。至少到目前为止对我来说这是可行的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2973165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档