简体   繁体   中英

C#/WinForms: how to prevent font changing based on OS language

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...

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 a Font explicitly in the forms designer, or — if you want the exact font determined at run-time — in the constructor after the call to InitializeComponent() . 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM