问题
I have some user controls in a windows form. I wonder
if i set the
Font
property of the main form, will its child get a) a copy of the newFont
, or b) a reference to the newFont
, or c) nothing?Does a font need to be disposed? For example, can I do the following code safely?
form.Font = new Font(...);
Will a font get disposed automatically when the parent (
Form
orUserControl
) is disposed?
Thanks,
Gilbert
回答1:
Both. The Font property is its own .NET object. Winforms however caches the native Windows font, they are fairly expensive to create. The .NET wrapper object is quite small.
Yes. The code is fine, the Font property setter already disposes the previously assigned font.
Yes, it is disposed by the UserControl. Which in turn is automatically disposed by its parent.
回答2:
- When
Font
property of the main form is set, its child controls get neither deep copy ofSystem.Drawing.Font
object nor the reference to it. On assignment, parent form creates unmanaged font object handle. All child controls are subscribed to parent’s FontChanged event so they have a chance to grab this handle and use it later for text rendering.Font
property getter of a control returns parent’s Font if control’s Font property has not been set explicitly (or default font). - Windows Forms does what is logically expected with any other property: When you assign a
System.Drawing.Font
object toFont
property of a control, a previously assignedSystem.Drawing.Font
object gets finalized and garbage-collected if no more references to it remain in your application. So yes, you can do this safely. System.Drawing.Font
object assigned toFont
property of a control is NOT getting disposed when the control is disposed, or anotherSystem.Drawing.Font
is assigned toFont
property. So you are safe to create a singleSystem.Drawing.Font
object and assign it to multiple controls, which are created and disposed dynamically. You are also free to create multiple fonts and assign them to a control periodically to create some visual effects. Internal unmanaged font handles are managed by Windows Forms.
回答3:
- If the child form is already open, no it won't get a copy.
- To expand a little on this if the child form does inherit the parents font. I don't think that changing the parents font would effect the child font after the window is already open.
- The garbage collector should take care of it.
- yes.
回答4:
From what I can tell, the Font property of a control is used to determine the font settings to use when drawing the control, but the GDI Font associated with that property isn't used to do the drawing. A control won't care if the Font that's assigned to its Font property is disposed after it's assigned, or even before it's assigned. The control is clearly capable of using some hidden aspects of the Font object which are available even after it is Disposed to determine the appropriate attributes of the font, but I don't know whether it
- Uses the supplied Font object, if not disposed, or else creates its own temporary font object each time it has to draw something and disposes the temporary object immediately thereafter.
- Uses the supplied Font object, if not disposed, or else creates its own private font object which it will dispose of when either the Font property is reassigned or the control is disposed.
- Copies the font family, size, etc. from the Font object when the Font property is assigned, and does everything with its private temporary or persistent Font objects it creates itself.
- Copies the font family, size, etc. from the assigned Font object whenever it's necessary to draw something.
The control certainly keeps a reference to the passed-in font object, if for no other reason than to supply it to the Font property getter. I have no idea, though, whether it's better to dispose the Font after assigning it, or whether it's better to keep a copy of the Font in the form and dispose it when the Form itself is disposed.
来源:https://stackoverflow.com/questions/5148476/windows-form-fonts-questions-part-1