开发者

Font Inheritance in Windows Forms

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

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 m开发者_如何学Goain window contains a set of controls including a menu strip and toolbar.

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?

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.

Thanks for your help in advance!


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();

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:

Font Inheritance in Windows Forms


As Neil said, you can pass the Font as a parameter of all child form.

But, you can also have a Static Class that will contain the font and all of your form can just Get the good font from it and apply it to itself. This could be set in the main form and read from all other forms.


Why not just make Font a parameter in the constructor of any spawned form?

When forms are spawned, they get the font passed in from he parent form, all set in the Main form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜