.net Windows 窗体应用程序的默认字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/297701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:38:30  来源:igfitidea点击:

Default font for Windows Forms application

.netwinformsuser-interfacefontsappearance

提问by wasker

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

每次我在我的应用程序中创建一个新表单时,它默认使用“Microsoft Sans Serif, 8.25pt”字体。我不会改变它,因为我知道在这种情况下我的表单应该选择系统的默认字体。但是,当我运行我的应用程序时,使用的字体仍然不是 Segoe UI(我的 Windows Vista 操作系统中的默认系统字体)。

Why does this happen? How do I make sure that my application looks like a normal Windows application?

为什么会发生这种情况?如何确保我的应用程序看起来像一个普通的 Windows 应用程序?

采纳答案by Robert Wagner

Check out this blog entrytalking about the default font in Forms which leads to the problem you are experiencing and this Connect Bugwith Microsoft's response. In short it just seems that Forms does not get the (correct) default windows font (which you have changed).

查看此博客条目,讨论 Forms 中的默认字体会导致您遇到的问题,以及这个Connect Bug与 Microsoft 的响应。简而言之,Forms 似乎没有获得(正确的)默认 Windows 字体(您已更改)。

回答by Cody Gray

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

其他一些答案提出了可靠的解决方法,但我发现最好的解决方案确实是创建一个基本表单,应用程序中的所有表单都继承自该基本表单,SystemFonts.MessageBoxFont并在构造函数中将此基本表单的 Font 属性设置为。这不仅可以确保您的应用程序在运行时根据用户环境选择正确的字体(避免 Hans Passant 带来的潜在问题——没有 Office 2007 的 XP 在没有 Segoe UI 的情况下将求助于 Microsoft Sans Serif ),而且还为您当前的 Windows 字体提供设计时支持。在设计时使用正确的字体解决了 Josuegomes 指出的问题,因为在表单上创建的任何容器控件都会在设计时选择表单使用的字体。

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.

除了上述优点之外,这使您不必记住为您创建的每个表单修改构造函数,并确保应用程序中所有表单的一致性,并为您提供放置其他常用功能的位置。我以几种不同的方式使用它,例如 p/invoking 等,以修复 WinForms 实现中的错误。

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)

回答by CS.

You can add before InitializeComponent() in the Form constructor(s):

您可以在 Form 构造函数中的 InitializeComponent() 之前添加:

this.Font = SystemFonts.MessageBoxFont;

This appear to work with Windows XP and Windows Vista.

这似乎适用于 Windows XP 和 Windows Vista。

回答by Hans Passant

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.

是的,它使用返回的字体GetStockObject(DEFAULT_GUI_FONT)。这是 MS Sans Serif。一种旧字体,在大多数机器上早已消失。毫不奇怪,字体映射器将其转换为 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.

我知道没有记录的程序可以更改该默认字体,SDK 文档明确提到了 MS Sans Serif。如果你想要 Segoe,你就得要求它。这不是那么安全,仍然有很多 XP 机器没有 Office 2007。字体映射器将在没有 Segoe 的机器上翻译它。不知道会弹出什么,我不再有这样的机器了。

回答by josuegomes

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.

如果您有带有关联控件的组框,则无法将表单的 Font 属性设置为 SystemFonts.DialogFont。组框内的控件不受窗体的 Font 属性的影响。我通过将每个组框的 Font 属性设置为 SystemFonts.DialogFont 来“解决”这个问题。

回答by David

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:

组框内的控件确实不受表单的 Font 属性的影响。原因是容器控件中的控件被视为容器控件(如 groupbox)的子项,而不是主窗体的子项。为了让所有控件(包括 groupboxes 中的控件)正确缩放,您可以使用类似于以下的代码:

        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;
                }
            }        
        }

回答by Pawat Dechachai

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.

试试这个,单击表单并更改字体大小,例如我将表单的字体大小更改为 12pt,然后通过将文本框拖动到表单来进行测试。您会看到,文本框大小也更改为 12pt。我只是偶然得到了这个解决方案。

回答by Adrian Rus

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

Control.DefaultFont是只读的; 一个hacky是覆盖它是使用反射。

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.

请务必让 UT 密切关注此代码,如果框架实现发生更改,则没有 API 合同可以保护您。

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

还要注意表单设计器,它大部分时间会在 .designer 类中逐字插入字体。

回答by Kirsan

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

我已经尝试了针对net472/net48和 的示例应用程序netcoreapp3.1。而 .net 应用程序Control.DefaultFont总是返回 Microsoft Sans Serif 而不是缩放。但是 .net core 3.1 应用程序Control.DefaultFont在 win7/10 上准确返回系统字体并且缩放良好。所以,我认为他们最终在核心中解决了这个问题。