Windows窗体字体问题第2部分 [英] Windows Form Fonts Questions Part 2
问题描述
如果我想将新用户控件的字体分配给其创建者的字体,该怎么办?我应该做这个a):
newControl = new MyControl(...);
...
newControl.Font = this.Font;
或者这个b)?
newControl = new MyControl(...);
...
newControl.Font =(Font)this.Font.Clone();如果答案是a),如果创建者的字体被处理,新的用户控件的字体会发生什么变化(例如,用户关闭的窗口)?
谢谢,
Gilbert
解决方案 $ b
-
文本样式
-
一个GDI句柄,可用于以该样式绘制文本
由 Font
类封装的文本样式是不可变的。句柄是一次性不可变的,意思是它永远不会封装除创建它之外的任何GDI句柄,但是一旦处理了 Font
,它就会停止封装任何字体句柄(它变成真正的不可变的,尽管在这一点上是无用的)。
设置 Font
属性在框架中的控件将导致它抓住两件事:
$ b
-
字体的标识
对象用于设置属性,该属性由 Font
属性getter
单独使用 >
-
控件用来创建自己的私有 Font
对象的文本样式。
由于每个Framework控件都会固有地克隆用于设置的任何 Font
> Font
属性,在用它来设置另一个控件的 Font
属性之前,通常不需要用户代码克隆字体。 b $ b
Continue to my previous question (link)
What if I want to assign the font of a new user control to its creator's font. Should I do this a):
newControl = new MyControl(...);
...
newControl.Font = this.Font;
or this b)?
newControl = new MyControl(...);
...
newControl.Font = (Font)this.Font.Clone();
If answer is a), what will happen to the new user control's font if the creator's font get disposed (for example, a window closed by user)?
Thanks,
Gilbert
解决方案 The Font
class actually encapsulates two things:
A text style
A GDI handle that may be used to draw text with that style
The text style encapsulated by a Font
class is immutable; the handle is "disposable immutable", meaning that it will never encapsulate any GDI handle other than the one with which it was created, but once the Font
is disposed it will cease to encapsulate any font handle (it becomes truly immutable, albeit useless, at that point).
Setting the Font
property of the controls in the Framework will cause it to grab two things:
The identity of the Font
object used to set the property, which is used solely by the Font
property getter
The text style, which the control will use to create its own private Font
object.
Because each Framework control will inherently clone any Font
instance used to set its Font
property, there's generally no need for user code to clone a font before using it to set another control's Font
property.
这篇关于Windows窗体字体问题第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文