如何强制 winforms 应用程序使用我在每台计算机上指定的字体/字体大小?

发布于 2024-09-05 20:02:46 字数 147 浏览 3 评论 0原文

如何强制 winforms 应用程序使用我在每台计算机上指定的字体/字体大小? 我有一个 ac# 应用程序,其中字体似乎在不同的机器上改变了大小,并且它不是分辨率。这是在 Windows 7 上使用 ac# 应用程序。有没有办法强制我想要的字体大小,或者用户操作系统字体总是否决?

How can I force a winforms app to use the fonts/fontsizes I specify on every machine?
I have a c# app where the font seem to change in size on a different machine, and it's not a resolution thig. This is on windows 7 with a c# app. Is there a way to force the font size I want or do the user os fonts always overrule?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2024-09-12 20:02:46

字体大小以点为单位指定。 1 点为 1/72 英寸。问题是这需要与 Windows 窗体的其余部分配合,其中几乎所有内容都以像素为单位指定。问题在于有多少像素构成一个点。这取决于您的视频适配器的每英寸点数设置。

传统设置是每英寸 96 点。因此,如果您要求使用 9 点字体,您将得到 9 / 72 * 96 = 12 像素高的字体。只要控件的客户区高度至少为 12 像素,其中显示的文本就不会被剪裁。

不过,视频适配器的 DPI 设置可以更改。它们在 XP 中变得有点困难,你必须进入显示小程序的高级设置才能执行此操作。在 Vista 中这变得容易多了,有一个漂亮的标尺之类的直接链接。

下一个常见设置是 120 DPI,即每英寸点数增加 125%。 9 点字体现在需要 9 / 72 * 120 = 15 像素。如果显示该文本的原始控件仍为 12 像素,则文本将被剪切。基本上,下降部分被剪掉。

因此,要解决此问题,需要更改表单中控件的 Control.Size 属性。它们需要进行缩放以适应更大的 DPI 设置。 Windows 窗体很容易支持这一点。它是 Form.AutoScaleMode,自 .NET 2.0 起可用。当您将其设置为 None 之外的其他值时,Windows 窗体内的自动代码就会启动,以补偿设计窗体的计算机和显示窗体的计算机之间的差异。在 120 DPI 的情况下,它会使窗体和控件变大。这样他们就可以显示文本而不会被剪切。

但是,有一个复杂的情况。更改 DPI 设置对于字体来说非常有用。 TrueType 是一项很棒的技术,能够呈现任何尺寸的漂亮字体。但对于包含线条艺术的图像,重新缩放效果不太好。当您重新缩放它们时,它们会产生额外的杂散像素或变得模糊。

回到字体:显然,如果更改显示器的 DPI 设置,用户会期望字体也会随之改变。这在 XP 时代确实是有效的。如果更改视频适配器的 DPI 设置,您会收到重新启动操作系统的提示。这在当时很重要,因为有很多程序使用设备字体,而不是 TrueType 字体。不可缩放字体。已激活与 DPI 设置匹配的新字体。因此,较大的视频 DPI 值与较大的系统基本字体大小相匹配。

大约在同一时间,微软的一位天才在被暗杀后想出了一个非常好的主意。生活在东亚的人们有基于字符的书写系统。复杂的字形需要大量像素来显示细节。 96 DPI 下的 12 像素并没有为制作美观的中文或韩文字形留下足够的空间。增加 DPI 显然可以改善这一点,但代价是图像模糊。想法:增加系统基本字体大小,但更改视频 DPI。

然而现在有一个新问题:如何自动缩放 UI。您可以在可以分配给 AutoScaleMode 属性的值(字体或 DPI)中看到这一点。哪一个对于目标机器来说是正确的不容易猜到。字体通常是更好的选择,因为您想确保文本不被剪切。但是,如果您想确保图像以正确的尺寸显示,那么您需要 DPI。当您两者都需要时,您就会陷入进退两难的境地,除非系统基本字体大小与视频 DPI 设置相匹配,否则您将无法摆脱困境。

Font sizes are specified in points. One point is 1/72 inches. The problem is that this needs to jive with the rest of Windows Forms where almost anything is specified in pixels. The issue is how many pixels make a point. That depends on your video adapter's dots-per-inch setting.

The traditional setting is 96 dots per inch. So if you ask for a 9 point font, you'll get one that's 9 / 72 * 96 = 12 pixels high. As long as the client area of a control is at least 12 pixels high, the text displayed inside of it won't be clipped.

The video adapter's DPI setting can be changed however. They made it kinda difficult in XP, you had to go into the Display applet's Advanced setting to do so. It got a lot easier in Vista, there's a direct link to it with a nice ruler and whatnot.

The next common setting is 120 DPI, 125% more dots per inch. That 9 point font now needs 9 / 72 * 120 = 15 pixels. If the original control that displays that text is still at 12 pixels, the text is going to get clipped. Basically, the descenders are sheared off.

So to fix that, the Control.Size property of the controls in the form need to change. They need to be scaled to accommodate the larger DPI setting. Windows Forms readily supports that. It is the Form.AutoScaleMode, available since .NET 2.0. When you set it to something else than None then automatic code inside Windows Forms kicks in that compensates for the difference between the machine that designed the form and the machine that displays the form. In the 120 DPI case, it will make the form and the controls bigger. So that they can display text without it getting clipped.

But, there's a complication. Changing the DPI setting is great for fonts. TrueType is awesome technology, capable of rendering good looking type at any size. But rescaling doesn't work very well for images that contain line art. They get stray extra pixels or get fuzzy when you rescale them.

Back to fonts: clearly if you change the DPI setting of the monitor, the user would expect the fonts to follow suit. That certainly worked that way back in the XP days. If you change the video adapter's DPI setting, you get a prompt to reboot the operating system. That was important back then because there were lots of program that used device fonts, not TrueType fonts. Non-scalable fonts. New fonts got activated that match the DPI setting. A larger value for video DPI is thus matched with a larger system base font size.

Around the same time, some kind of genius at Microsoft, since assassinated, came up with a Really Good Idea. People that live in East Asia have a writing system based on characters. Intricate glyphs that need lots of pixels to show the details. 12 pixels at 96 DPI does not leave a lot of room to make a good looking Chinese or Korean glyph. Increasing the DPI obviously improves that, but at the cost of having fuzzy images. The Idea: increase the system base font size but don't change the video DPI.

Now there's a new problem however: how to auto-scale the UI. You can see this back in the values you can assign to the AutoScaleMode property, either Font or DPI. Which one is correct for the target machine is not easily guessable. Font is normally the better choice since you want to make sure that text isn't clipped. However, if you want to make sure that images display at the right size, then you want DPI. When you want both then you are stuck between a rock and a hard place, one you can't get out of unless the system base font size matches the video DPI setting.

情深如许 2024-09-12 20:02:46

除了指定您喜欢的字体大小之外,您不能做更多的事情。如果用户选择使用较大的字体大小 (125% / 150%) 或放大镜,您的字体大小将会增大。

You cannot do more than specify the font size you prefer. If the user chooses to use great font sizes (125% / 150%) or the magnifier, your font size will increase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文