Every time that I create a new form in my application, it uses the "Microsoft Sans Serif, 8.25pt" font by default. I'm not changing it because I know that in this case my form should pick up whatever the default font is for the system. However, when I run my application, the font that is used is still anything but Segoe UI (my default system font in my Windows Vista OS).
Why does this happen? How do I make sure that my application looks like a normal Windows application?
If the above fonts are not installed, the default font is Tahoma, 8 point.
Even though the default fonts provided by Microsoft—Segoe UI for Windows 10, and Segoe UI variable for Windows 11—looks pretty neat on the screen, you don't have to settle if you have grown bored with them; especially when you can easily alter them with the Windows Registry.
In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units. Because a device-independent unit is 1/96 inch, a FontSize value of 12 represents characters whose capitals are 1/8″ high.
The accepted answer doesn't really answer the question; it just explains why this behavior is occurring.
Some of the other answers propose solid workarounds, but I've found that the best solution really is to create a base form that all of the forms in your application inherit from and set this base form's Font property to SystemFonts.MessageBoxFont
in the constructor. This not only ensures that your application picks up the correct font at run-time, based on the user's environment (heading off the potential problem posed by Hans Passant—an XP without Office 2007 will resort to Microsoft Sans Serif in the absence of Segoe UI), but also gives you design-time support for your current Windows font. Using the correct font at design time solves the problem Josuegomes points out, because any container control that is created on the form will pick up the font used by the form at design-time.
Besides the above advantages, this frees you from having to remember to modify the constructor for each form that you create and ensures consistency across all of the forms in your application, as well as giving you a place to put other common functionality. I use this in a couple of different ways such as p/invoking, etc. to fix bugs in the WinForms implementation.
The only problem that remains with this approach is if you want to set a font style for a particular control, such as bold. The best place to do this is still in that form's constructor, starting with the form's font as a base and modifying the style from it:
myControl.Font = New Font(Me.Font, FontStyle.Bold)
You can add before InitializeComponent() in the Form constructor(s):
this.Font = SystemFonts.MessageBoxFont;
This appear to work with Windows XP and Windows Vista.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With