开发者

Scaling a Control's Font in C# .Net

We all know that the Control base class has a Scale method. The problem is this: it's absolute crap. While it admirably scales all of开发者_JS百科 the controls, it does not scale the fonts. So all my my labels, text boxes, and buttons look so very awkward. It would be so simple to just loops through all of the form's controls and change the font, but Control.Font.Size is read-only so foreach(Control C in this.Controls){C.Font.Size = C.Font.Size * .8f;} won't do the trick.

What is the best way to scale the font of every control in a form when the form itself is scaled?


To change the font size of a control, you have to create a new Font object and assign it to the control.

You can use the Font object in the existing control as a template to create the new Font object, while changing only the font properties you want, thusly:

myControl.Font = new Font(myControl.Font, 
    myControl.Font.Style | FontStyle.Bold);

There's also a font scaling tutorial with some sample code here: http://www.switchonthecode.com/tutorials/csharp-tutorial-font-scaling


Font scaling is not that easy, because you cannot control the height and width, you have only one property: Size.

What I normally do is use the TableLayoutPanel intensively. You can then set the font Size, and all controls should (if used Docking and AutoSize properly) scale nicely.

Oh, and change the font size by setting a the Font property of the Form to a new Font(...)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜