AutoScaleMode问题更改默认字体 [英] AutoScaleMode problems with changed default font

查看:465
本文介绍了AutoScaleMode问题更改默认字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题Form.AutoScaleMode财产连同固定大小的控制,使用非默认字体的时候。我煮沸它下降到一个简单的测试应用(的WinForms 2.0)只有一种形式中,一些固定尺寸的控制和以下性能:



  Form1类:表格
{
// ...
私人无效的Ini​​tializeComponent()
{
// ...
this.AutoScaleDimensions =新System.Drawing.SizeF(96F,96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.Font =新System.Drawing.Font(宋体,9.25F);
// ...
}
}



在96DPI ,Windows XP中,形式看起来正确像这样96 dpi的例子:



96



在120 dpi的,Windows XP中,Windows窗体自动缩放功能产生这个120 dpi的例子:





正如你所看到的,groupboxes,按钮,列表或树状视图正确缩放,多行文本框获得在垂直轴上太大,而一个固定大小的标签不垂直和水平方向的正确缩放。似乎是在.NET框架的错误



编辑:?一些提示:字体变化仅适用于包含窗体,控件继承形式的字体。我想,如果能够保持这种方式。



使用默认字体(Microsoft无衬线8.25pt),不会出现此问题。使用 AutoScaleMode =字体(有足够的AutoScaleDimensions,当然),要么没有规模可言,或鳞以上酷似看出,这取决于当字体设置上(之前或之后AutoScaleMode的变化)。问题不在于具体的宋体字体,它会与Microsoft无衬线,9.25pt也会发生。



是的,我已经阅读SO张贴
高DPI问题
,但它并没有真正帮助我。



任何建议如何来解决这个



EDIT2:我的本意​​一些额外的信息:我有大约50已经工作的固定大小的对话框与几百妥善安置,固定大小的控制。他们从旧的C ++图形用户界面框架,C#/的WinForms已迁移,这就是为什么它们都是固定大小。他们都看起来很好用一个9.25pt字体96 DPI。在旧的框架,扩展到120 dpi的工作得很好 - 所有的固定尺寸控制在缩放两个维度相等。上周,我们切换到120 DPI检测时,根据的WinForms这种奇怪的缩放行为。你可以想像,我们大部分的对话框现在在120 dpi的看起来很糟糕。我在找,避免完全重新设计所有这些对话的解决方案



EDIT3:为了检验这一行为,恕我直言,这是建立一个虚拟的Windows XP是个好主意环境120 DPI,而开发环境下居住96 DPI(至少,这就是我所做的)。改变96至120 DPI通常需要在Win XP下重新启动,否则你看不到到底发生了什么。

  //根据要求:Form1.cs的
命名空间DpiChangeTest
{
公共部分Form1类的源代码:表单
{
公共Form1中()
$ { b $ b的InitializeComponent();
字体F = this.textBox1.Font;
}
}
}

//这里在Form1.designer.cs来源:
命名空间DpiChangeTest
{
部分Form1类
{
私人System.ComponentModel.IContainer成分= NULL;

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
组分。的Dispose();
}
base.Dispose(处置);
}

#地区的Windows窗体设计器生成的代码

私人无效的Ini​​tializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem2 =新System.Windows.Forms.ListViewItem(A列表视图控件);
System.Windows.Forms.TreeNode treeNode2 =新System.Windows.Forms.TreeNode(TreeView控件);
this.button1 =新System.Windows.Forms.Button();
this.groupBox1 =新System.Windows.Forms.GroupBox();
this.textBox1 =新System.Windows.Forms.TextBox();
this.label1 =新System.Windows.Forms.Label();
this.listView1 =新System.Windows.Forms.ListView();
this.treeView1 =新System.Windows.Forms.TreeView();
this.SuspendLayout();
//
//按钮1
//
this.button1.Location =新System.Drawing.Point(12,107);
this.button1.Name =Button1的;
this.button1.Size =新System.Drawing.Size(150,70);
this.button1.TabIndex = 0;
this.button1.Text =只是一个按钮;
this.button1.UseVisualStyleBackColor = TRUE;
//
// groupBox1
//
this.groupBox1.Location =新System.Drawing.Point(12,12);
this.groupBox1.Name =groupBox1;
this.groupBox1.Size =新System.Drawing.Size(150,70);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = FALSE;
this.groupBox1.Text =只是一个组框;
//
// textBox1的
//
this.textBox1.Location =新System.Drawing.Point(180,12);
this.textBox1.Multiline = TRUE;
this.textBox1.Name =textBox1的;
this.textBox1.Size =新System.Drawing.Size(150,70);
this.textBox1.TabIndex = 2;
this.textBox1.Text =多行文本框;
//
// LABEL1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location =新System.Drawing.Point(179,107);
this.label1.Name =LABEL1;
this.label1.Size =新System.Drawing.Size(150,70);
this.label1.TabIndex = 3;
this.label1.Text =A自动调整大小=假标签;
//
// ListView1的
//
this.listView1.Items.AddRange(新System.Windows.Forms.ListViewItem [] {
listViewItem2});
this.listView1.Location =新System.Drawing.Point(12,201);
this.listView1.Name =ListView1的;
this.listView1.Size =新System.Drawing.Size(150,70);
this.listView1.TabIndex = 4;
this.listView1.UseCompatibleStateImageBehavior = FALSE;
//
//为TreeView1
//
this.treeView1.Location =新System.Drawing.Point(179,201);
this.treeView1.Name =为TreeView1;
treeNode2.Name =Knoten0;
treeNode2.Text =TreeView控件
this.treeView1.Nodes.AddRange(新System.Windows.Forms.TreeNode [] {
treeNode2});
this.treeView1.Size =新System.Drawing.Size(150,70);
this.treeView1.TabIndex = 5;
//
// Form1中
//
this.AutoScaleDimensions =新System.Drawing.SizeF(96F,96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize =新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 =新System.Drawing.Font(Microsoft无衬线,9.25F);
this.Name =Form1的;
this.Text =Form1的;
this.ResumeLayout(假);
this.PerformLayout();

}

#endregion

私人System.Windows.Forms.Button按钮1;
私人System.Windows.Forms.GroupBox groupBox1;
私人System.Windows.Forms.TextBox textBox1的;
私人System.Windows.Forms.Label LABEL1;
私人System.Windows.Forms.ListView ListView1的;
私人System.Windows.Forms.TreeView为TreeView1;
}
}

//和Main.cs
[STAThread]
静态无效的主要()
{
应用.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新Form1中());
}


解决方案

我终于找到了答案我的问题。总之,当一个设置每个控件的字体分别代替设置含有形式的字体不发生的效果。通过这种方式,自动缩放功能的工作原理,因为它应该。有趣的是,设置控件的字体的变化,即使AutoScaleMode属性设置为 AutoScaleMode.Dpi 自动缩放行为,而不是只有当它设置为 AutoScaleMode.Font



作为一个务实的解决方案,我们创建了一个小型命令行程序,它读取designer.cs文件,扫描如果所有控件都具有明确的字体的设置,如果没有,增加分配到设计代码的一个新创建的副本。我们这个节目嵌入到我们的自动测试套件,所以每当一个形式得到新的控件,或者添加了新的形式,并开发忘记添加明确字体分配,测试将失败。在这期间,我们有这个解决方案,从我第一次问这个问题(4年前),它从从那时起缩放几次的问题为我们节省时间的工作。


I have some problems with the Form.AutoScaleMode property together with fixed size controls, when using a non-default font. I boiled it down to a simple test application (WinForms 2.0) with only one form, some fixed size controls and the following properties:

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);
        // ...
    }
}

Under 96dpi, Windows XP, the form looks correctly like this 96 dpi example:

Under 120 dpi, Windows XP, the the Windows Forms autoscaling feature produces this 120 dpi example:

As you can see, groupboxes, buttons, list or tree views are scaled correctly, multiline text boxes get too big in the vertical axis, and a fixed size label does not scale correctly in both vertical and horizontal direction. Seems to be bug in the .NET framework?

EDIT: some hints: the Font change is only applied to the containing form, the controls inherit their font from the form. I would like to keep it this way, if possible.

Using the default font (Microsoft Sans Serif 8.25pt), this problem does not occur. Using AutoScaleMode = Font (with adequate AutoScaleDimensions, of course) either does not scale at all or scales exactly like seen above, depending on when the Font is set (before or after the change of AutoScaleMode). The problem is not specific to the "Tahoma" Font, it occurs also with Microsoft Sans Serif, 9.25pt.

And yes, i already read this SO post high DPI problems but it does not really help me.

Any suggestions how to come around this?

EDIT2: Some additional information about my intention: I have about 50 already working fixed size dialogs with several hundreds of properly placed, fixed size controls. They were migrated from an older C++ GUI framework to C#/Winforms, that's why they are all fixed-size. All of them look fine with 96 dpi using a 9.25pt font. Under the old framework, scaling to 120 dpi worked fine - all fixed size controls scaled equal in both dimensions. Last week, we detected this strange scaling behaviour under WinForms when switching to 120 dpi. You can imagine that most of our dialogs now look very bad under 120 dpi. I am looking for a solution that avoids a complete redesign all those dialogs.

EDIT3: To test this behaviour, IMHO it is a good idea to set up a virtual Windows XP environment with 120 dpi while the development environment resides under 96 dpi (at least, that's what I did). Changing between 96 and 120 dpi normally needs a reboot under Win XP, otherwise you don't see what really happens.

// 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());
    }

解决方案

I finally found an answer for my question. In short, the effect does not arise when one sets the font of each control individually instead of setting the font of the containing form. This way, the auto scaling feature works as as it should. Interestingly, setting the font of the controls changes the auto-scaling behaviour even if the AutoScaleMode property is set to AutoScaleMode.Dpi, not only when it is set to AutoScaleMode.Font.

As a pragmatic solution, we created a small command line program, which reads the designer.cs files, scans if all controls have an explicit font assignment, and if not, adds the assignment into a newly created copy of the designer code. We embedded this program into our automatic test suite, so whenever a form gets new controls, or a new form is added, and the dev forgets to add the explicit font assignment, the tests will fail. In between, we have this solution working from the time I asked this question first (4 years ago), and it saved us from scaling problems for several times since then.

这篇关于AutoScaleMode问题更改默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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