检测 Windows 字体大小(100%、125% 和 150%) [英] Detect Windows font size (100%, 125%, and 150%)

查看:79
本文介绍了检测 Windows 字体大小(100%、125% 和 150%)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在拥有中文版 Windows 7 的人开始使用我的应用程序之前,这一直很有效.整个应用程序在中文 Windows 7 上中断.据我所知(我无法真正测试它,因为我只有英文版本并且安装语言包不会导致问题)中文字符导致奇怪的 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.

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

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
}

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

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.

那么在不检测 DPI 的情况下检测 Windows 字体比例(100%、125% 和 150%)的好方法是什么?我需要一些可以在所有 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 设置的正确方法不是检测它们并在 switch 语句中手动调整控件的大小(对于首先,比您在示例 if 语句中显示的可能性要多得多).

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

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

Instead, you should set the AutoScaleMode property of your form to AutoScaleMode.Dpi and let the framework take care of this for you.

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

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

this.AutoScaleMode = AutoScaleMode.Dpi;

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

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

这篇关于检测 Windows 字体大小(100%、125% 和 150%)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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