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

查看:361
本文介绍了检测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.

我当前的代码如下:

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天全站免登陆