【发布时间】:2019-03-03 23:01:54
【问题描述】:
我把它归结为一个非常简单的复制品,但无法弄清楚这个表格出了什么问题。当以 96 DPI / 100% 比例运行时,它看起来很好:
但是当以 144 DPI / 150% 比例(甚至 96 DPI / 150% 比例)运行时,除了表单高度之外,所有内容都会缩放:
最初我认为这是一个 DPI 问题,但在验证它以 96 DPI 重现后,我不确定发生了什么。
除了专门设置对话框的字体并将 AutoScaleMode 设置为 DPI 之外,对话框或控件没有什么特别之处。表单位于应用程序自动加载的程序集中。
我使用的是 .NET 4.7.2 和 Windows 10。
这是表单代码:
using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormTestLib
{
partial class ValidatingSplash : Form
{
public ValidatingSplash()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.CenterToParent();
}
}
}
这是设计器文件:
namespace FormTestLib
{
public partial class ValidatingSplash
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ValidatingSplash));
this.lblValidating = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblValidating
//
this.lblValidating.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblValidating.AutoSize = true;
this.lblValidating.Location = new System.Drawing.Point(58, 45);
this.lblValidating.Name = "lblValidating";
this.lblValidating.Size = new System.Drawing.Size(166, 13);
this.lblValidating.TabIndex = 7;
this.lblValidating.Text = "Validating cached credentials...";
//
// ValidatingSplash
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(274, 104);
this.ControlBox = false;
this.Controls.Add(this.lblValidating);
this.Font = new System.Drawing.Font("Segoe UI", 8.25F);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ValidatingSplash";
this.Text = "Validating Credentials";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblValidating;
}
}
在 app.config 中,我根据文档设置 DpiAwareness:
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2"/>
</System.Windows.Forms.ApplicationConfigurationSection>
在清单中我设置了兼容性:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 compatibility -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
所有这些都根据高 DPI 支持here 的说明进行。
应用程序代码只是显示对话框:
namespace TestApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// set the visual styles
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
ValidatingSplash Splash = new ValidatingSplash();
Splash.ShowDialog();
}
}
}
谁能看到我可能做错了什么,或者我错过了什么?
提前致谢!
【问题讨论】:
-
删除
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);。您可以在app.manifest中找到 dpiAwareness 设置。即使不再推荐,也要试试。取消评论您喜欢的内容。我怀疑你在文档中找到了这个:Application.SetCompatibleTextRenderingDefault(true);。 -
如果您在启动程序之前更改 DPI,或者仅在程序运行时更改缩放比例时,它会这样做吗?
-
另外,这个问题是一个有用的资源:@987654324@
-
我在设计器代码中发现了有问题的行:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;如果该行被注释掉,一切都会正常运行。当设置了特定的对话框字体并且启用了 DPI 感知和显示缩放时(或有缺陷的情况下),不支持此边框样式。谁能确认这确实是一个错误还是设计使然?我无法在文档中找到任何内容。 -
奖励:仅当主显示器被缩放时才会出现此问题。在这种情况下,所有显示器都会出现问题(无论每台显示器的比例如何)。如果主显示器未缩放,则任何监视器(甚至缩放监视器)都不会出现问题。此外,无论字体是否被显式设置,都会发生这种情况。也就是说,它也会出现在默认的系统字体中。
标签: c# .net winforms highdpi dpi-aware