当使用非默认字体时,Form.AutoScaleMode属性和固定大小的控件一起出现了一些问题。我煮沸它归结为一个简单的测试应用(的WinForms 2.0)只有一种形式中,一些固定尺寸的控制和以下性质:AutoScaleMode更改默认字体的问题
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,形式看起来正确地像这样96 DPI例如:
在120 DPI,Windows XP中,Windows窗体自动缩放功能产生本120 DPI例如:
正如您所看到的,组合框,按钮,列表或树视图正确缩放,多行文本框在垂直轴上变得太大,而固定大小的标签在垂直和水平方向上都不能正确缩放。似乎是在.NET框架中的错误?
编辑:一些提示:字体更改只适用于包含窗体,控件从窗体继承它们的字体。如果可能,我想保持这种方式。
使用默认字体(Microsoft Sans Serif 8.25pt),不会发生此问题。使用AutoScaleMode = Font
(当然具有足够的AutoScaleDimensions)根据Font设置的时间(在AutoScaleMode更改之前或之后),根本不会缩放或缩放。该问题不是特定于“Tahoma”字体,它也发生在Microsoft Sans Serif 9.25pt。
是的,我已经读过这个SO帖子 high DPI problems 但它并没有真正帮助我。
任何建议如何来解决这个问题?
编辑2:关于我的意图的一些额外信息:我已经有大约50个工作的固定大小的对话框,有几百个正确放置的固定大小的控件。他们从旧的C++ GUI框架迁移到C#/ Winforms,这就是为什么他们都是固定大小。所有这些都使用9.25pt字体在96 dpi下显得很好。在旧的框架下,缩放到120 dpi的效果很好 - 所有固定大小的控件在两个维度上缩放比例相等。上周,我们在切换到120 dpi时在WinForms下检测到了这种奇怪的缩放行为。你可以想象,现在我们的大多数对话框在120dpi以下看起来都很糟糕。我正在寻找一种解决方案,避免完全重新设计所有这些对话框。
EDIT3:为了测试这种行为,恕我直言,当开发环境位于96 dpi(至少,这就是我所做的)时,建立一个120 dpi的虚拟Windows XP环境是个好主意。通常需要在Win XP下重新启动96到120 dpi,否则你不会看到真正发生的事情。
// 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());
}
我遇到了同样的问题。控制不响应'Scale'(或受保护的“ScaleControl”)方法来缩放其'Font'。我正在玩的解决方案是递归迭代所有控件,并且(神奇地)确定字体是否应该是什么,并修复它。尽管如此,我仍然在努力研究一些陷阱。如果我得到解决方案,会报告回来 – 2011-11-24 19:14:36
我感到你的痛苦。我在同一条船上,似乎这是一个普遍而且非常严重的问题,但似乎没有人解决它或提供答案。 – 2012-04-09 18:02:18
@ShacharWeis:对于我们的情况,我们有一个解决方案。我们创建了一个小程序(小于200 LOC),它读取designer.cs文件,扫描所有控件是否具有明确的字体分配,如果没有,则将该分配添加到代码中。 – 2012-04-16 16:11:57