在Windows窗体字体传承 [英] Font Inheritance in Windows Forms

查看:134
本文介绍了在Windows窗体字体传承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能与一个简单的回答愚蠢的问题,但我画一个空白在这里,他会喜欢一些帮助。

This is probably a silly question with a trivial answer, but I'm drawing a blank here and would love some assistance.

假如我有两种形式在Windows窗体应用程序:主窗口,这将显示为第一个对话框的子窗口。主窗口包含了一组控件,包括菜单条和工具栏。

Suppose I have two forms in a Windows Forms Application: the main window and a child window that will displayed as a dialog of the first. The main window contains a set of controls including a menu strip and toolbar.

我怎么能保证在两种形式的所有控件将使用相同​​的,用户可配置的字体?使用VS​​设计测试这个真的很快,我的主要形式的字体属性设置为比默认的东西等,并确保所有在该表单控件设置的默认值。这样做会导致一些控件相应调整,但菜单条和工具栏的字体保持不变/默认。这是由设计,这些控件或者是有什么明显的是我丢失吗?

How can I ensure that all controls in both forms will use the same, user-configurable font? Testing this really quickly using the VS designer, I set the Font property of the main form to something other than the default and ensure that all of the controls in that form are set with the default. Doing so results in some controls adjusting accordingly, but the font of the menu strip and toolbar remain the same/default. Is this by design with those controls or is there something obvious that I'm missing?

对于第二种形式,我展示这种形式的主要形式的子对话框。除非我明确地设置孩子的字体形式,它无法显示使用相同的字体为主要形式(尽管被配置了默认字体)。在设置对话框窗口的字体以匹配父正确的做法还是我失去了一些东西?

As for the second form, I am displaying this form a child dialog of the main form. Unless I explicitly set the font of the child form it fails to display using the same font as the main form (despite being configured with the default font). Is setting the font of dialog windows to match the parent the correct approach or am I missing something?

此外,仅仅是明确的。这里只有一个地方字体属性被设置在我的code点和它的主要形式。我并没有因为控制/形式明确设置的问题。

Again, just to be clear. There's only one spot in my code where the Font property is being set and it's on the main form. I'm not having issues because controls/forms are set explicitly.

感谢您的帮助提前!

推荐答案

这是不太继承,该功能被称为环境属性。当字体属性尚未分配,无论是在设计器(如图粗体),或在您的code则控制使用其父母的字体属性。这是非常方便的,它允许一个非常一致的外观和感觉,并有助于让布局上有不同的视频点每英寸的设定运行的机器是一致的。其他的环境属性背景色,前景色,光标,从右至左。有管道,以确保这仍然有效,如果家长改变或家长改变其属性值。

It is not quite inheritance, the feature is called 'ambient property'. When the Font property hasn't been assigned, either in the designer (shown in bold) or in your code then the control uses the Font property of its Parent. Which is very convenient, it allows for a very consistent look-and-feel and helps getting layout consistent on machines that run with a different video dots-per-inch setting. Other ambient properties are BackColor, ForeColor, Cursor, RightToLeft. There's plumbing to make sure this still works if the Parent changed or the parent changes its property value.

接下来是责无旁贷的时候没有父母了。这发生于形式,它没有一个家长。因此,它是由你来,如果你想使用一个值,是从你的设计师挑一个不同的窗体的字体属性分配。这很容易做到:

What follows is that the buck stops when there is no Parent anymore. Which happens for Form, it doesn't have a Parent. So it is up to you to assign the Form's Font property if you want to use a value that's different from the one you picked in the designer. Easy to do:

var frm = new Form2();
frm.Font = this.Font;
frm.Show();

ToolStrip的和MenuStrip中类的行为有点不同。除非EX pressly分配,他们初始化它们的字体从系统设置。用户可以选择,她prefers在控制面板中显示小程序(个性化在Windows 7)字体和字号。覆盖这个选择当然是可能的,但通常不应该这样做并尊重用户的preference。控制面板对话框如下所示:

The ToolStrip and MenuStrip classes behave a bit differently. Unless expressly assigned, they initialize their Font from a system setting. The user can pick the font face and point size that she prefers in the Display applet in Control Panel (Personalization in Windows 7). Overriding this selection is certainly possible, but you generally should not do this and honor the user's preference. The control panel dialog looks like this:

这篇关于在Windows窗体字体传承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆