C# 检测 Windows 字体大小(100%、125% 和 150%)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9373260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:05:06  来源:igfitidea点击:

Detect Windows font size (100%, 125%, and 150%)

c#.netwinformswindows-7cjk

提问by Landin Martens

I created an application that works perfectly until the user selects 125% or 150%. It would break my application. I later found a way to find the font size by detecting the DPI.

我创建了一个完美运行的应用程序,直到用户选择 125% 或 150%。它会破坏我的应用程序。后来我找到了一种通过检测 DPI 来找到字体大小的方法。

This was working great until people with Chinese versions of Windows 7 started using my application. The entire application breaks on Chinese Windows 7. From what I can tell (I can't really test it for I only have the English version and installation the language packs does not cause the problem) Chinese characters are causing a weird DPI that breaks my application.

在拥有中文版 Windows 7 的人开始使用我的应用程序之前,这非常有效。整个应用程序在中文 Windows 7 上中断。据我所知(我无法真正测试它,因为我只有英文版本并且安装语言包不会导致问题)中文字符导致奇怪的 DPI 中断了我的应用。

My current code works like this:

我当前的代码是这样工作的:

if (dpi.DpiX == 120) // For 125% fonts
{
    // Resize form and set default font to correct problems
}
else if (dpi.DpiX == 96) // For 100 and 150% fonts
{
    // Resize form and set default font to correct problems
}

On English versions of Windows 7 that works great, but somehow Chinese versions skip right by this, and the form destroys itself, with controls not even showing up, font extremely large and pushing past the problem, picture boxes being moved around.

在英文版的 Windows 7 上效果很好,但不知何故中文版直接跳过了这个,表格会自我破坏,甚至没有显示控件,字体非常大并且解决了问题,图片框被移动了。

So what is a good way to detect the Windows font scale (100%, 125%, and 150%) without detecting DPI? I need something solid that will work on all Windows 7 operating systems and languages.

那么在不检测 DPI 的情况下检测 Windows 字体比例(100%、125% 和 150%)的好方法是什么?我需要一些可以在所有 Windows 7 操作系统和语言上运行的可靠东西。

采纳答案by Cody Gray

The correct way of handling variable DPI settings is not to detect them and adjust your controls' sizes manually in a switchstatement (for starters, there are far more possibilities than those you show in your sample ifstatement).

处理可变 DPI 设置的正确方法不是检测它们并在switch语句中手动调整控件的大小(对于初学者来说,比您在示例if语句中显示的可能性要多得多)。

Instead, you should set the AutoScaleModepropertyof your form to AutoScaleMode.Dpiand let the framework take care of this for you.

相反,您应该将表单的AutoScaleMode属性设置为AutoScaleMode.Dpi并让框架为您处理。

Add the following code to your form's constructor (or set this property at design time):

将以下代码添加到表单的构造函数中(或在设计时设置此属性):

this.AutoScaleMode = AutoScaleMode.Dpi;

Although you might prefer to use AutoScaleMode.Font. For more information on automatic scaling, see the MSDN documentation.

尽管您可能更喜欢使用AutoScaleMode.Font. 有关自动缩放的更多信息,请参阅MSDN 文档

回答by nspire

For C++/Win32 users, here is a good reference: Writing High-DPI Win32 Applications.

对于 C++/Win32 用户,这里有一个很好的参考:编写高 DPI Win32 应用程序

回答by user11581995

if your on a newer version of windows I recommend reinstalling your graphics card drivers ( e.g installing a newer version) I had the same problem, my display scale was set to 100% but the font was way off. hope this fixes your problem

如果您使用的是较新版本的 Windows,我建议您重新安装您的显卡驱动程序(例如安装较新版本)。我遇到了同样的问题,我的显示比例设置为 100%,但字体偏离了很多。希望这能解决你的问题