可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
Check out this blog entry talking about the default font in Forms which leads to the problem you are experiencing and this Connect Bug with Microsoft's response. In short it just seems that Forms does not get the (correct) default windows font (which you have changed).
回答2:
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)
回答3:
You can add before InitializeComponent() in the Form constructor(s):
this.Font = SystemFonts.MessageBoxFont;
This appear to work with Windows XP and Windows Vista.
回答4:
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.
回答5:
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.
回答6:
The controls inside the group box are indeed not affected by the form's Font property. The reason is that controls in container controls are treated as children of the container controls like groupbox, but not children of the main form. In order for all controls including those in groupboxes to scale properly, you can use code similar to below:
foreach (Control ctr in this.Controls)
{
ctr.Font = SystemFonts.IconTitleFont;
// controls in groupboxes are not child of main form
if (ctr.HasChildren)
{
foreach (Control childControl in ctr.Controls)
{
childControl.Font = SystemFonts.IconTitleFont;
}
}
}
回答7:
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.
回答8:
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.