C#/WinForms:如何防止基于操作系统语言的字体更改
我用 C# 编写了一个 WinForms 程序,并为所有表单控件(标签、复选框...)保留了默认字体(我认为该字体是 Tahoma,但不确定)。当我编译该程序然后在日语计算机上运行它时,所有字体都会切换为“MS UI Gothic”(日语字体)。
无论如何,我可以强制所有平台上的字体相同吗?目前它使布局变得很混乱,因为字符根据字体有不同的宽度......
I have written a WinForms program in C# and left the default font for all form controls (labels, checkboxes...) (The font is Tahoma I think, not sure though). When I compile the program and then run it on a Japanese language computer, all the fonts switch to "MS UI Gothic" (a Japanese font).
Is there anyway I can force the font to be the same on all platforms? Currently it messes up the layout a lot, as the characters have a different width depending on the font...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如评论中已经提到的,真正的解决方案是确保您的表单布局可以处理不同的字体和字体大小。毕竟,即使是英语用户有时也会更改控制面板中的字体大小。
也就是说,您可以通过在表单上设置
Font
属性来强制使用字体。大多数(如果不是全部)控件将从窗体继承字体,如果它们没有自己设置的显式字体。因此,您可以在表单设计器中显式设置Font
,或者(如果您希望在运行时确定确切的字体)在调用InitializeComponent()
。但这样做时要注意: ① 如果该字体在另一台机器上不存在,则会引发异常; ② 你会让你的软件在未来更难国际化。As has already been mentioned in the comments, the real solution is to make sure that your form layout can handle different fonts and font sizes. After all, even English users will sometimes change the font size in their Control Panel.
That said, you can force a font by setting the
Font
property on the form. Most (if not all) controls will inherit the font from the Form if they don’t have an explicit font of their own set. Thus, you could set aFont
explicitly in the forms designer, or — if you want the exact font determined at run-time — in the constructor after the call toInitializeComponent()
. But when you do this, be aware that ① this will cause an exception if the font doesn’t exist on another machine; and ② you are making it harder for your software to be internationalised in the future.您可以通过 Font 属性显式设置表单中的字体。但是,我不确定如果计算机上没有该字体,会有什么反应。
You can set the font in the form explicitly, via the Font property. I am not sure what the reaction will be if the font is not available on the computer, however.
如果您的程序要在不同语言的操作系统上运行,您应该预先尽可能多地考虑到这一点。这是来自 MSDN 的一篇很好的入门文章 - 全球化逐步特别是全球化分步字体
If your program is going to run on a different-language OS, you should account for this as much as possible up-front. Here's a good starter article from MSDN - Globalization Step-by-Step and in particular Globalization Step-by-Step Fonts