Default font for Windows Forms application

后端 未结 9 1340
有刺的猬
有刺的猬 2020-12-08 07:06

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

相关标签:
9条回答
  • 2020-12-08 07:16

    Yes, it uses the font returned by GetStockObject(DEFAULT_GUI_FONT). Which is MS Sans Serif. An old font, long gone from most machines. The font mapper translate it to, no surprise, Microsoft Sans Serif.

    There is no documented procedure I know of to change that default font, the SDK docs mention MS Sans Serif explicitly. If you want Segoe, you'll have to ask for it. Which isn't that safe to do, there are still a lot of XP machines out there without Office 2007. The font mapper will translate it on a machine that doesn't have Segoe available. Not sure what pops out, I don't have such a machine left anymore.

    0 讨论(0)
  • 2020-12-08 07:20

    Try this, Click a Form and change font size for example I changed a font size of Form to 12pt and then test by drag text box to the Form. You'll see, The textbox size is changed to 12pt as well. I've just got this solution by accident.

    0 讨论(0)
  • 2020-12-08 07:28

    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)
    
    0 讨论(0)
  • 2020-12-08 07:28

    Setting the form's Font property to SystemFonts.DialogFont doesn't work if you have group boxes with associated controls. The controls inside the group box are not affected by the form's Font property. I "solved" this by setting the Font property to SystemFonts.DialogFont for each and every group box.

    0 讨论(0)
  • 2020-12-08 07:28

    The Control.DefaultFont is ReadOnly; one hacky was to overwrite it is to use reflection.

    Type settingsType = typeof(Control);
    var defaultFontField = settingsType.GetField("defaultFont", BindingFlags.Static | BindingFlags.NonPublic);
    defaultFontField.SetValue(null, new Font("Segoe UI", 8.25F));
    

    Be sure to have a UT keeping an eye on this code, there is no API contract to protect you if the Framework implementation changes.

    Also be aware of forms designer which most of the time will insert the font verbatim in .designer classes.

    0 讨论(0)
  • 2020-12-08 07:30

    I've tried sample app that targets both net472/net48 and netcoreapp3.1. While .net app Control.DefaultFont always returns Microsoft Sans Serif and not scaled. But .net core 3.1 app Control.DefaultFont returns exactly the system font on win7/10 and scaled well. So, I think they fixed this in core at last.

    0 讨论(0)
提交回复
热议问题