Windows Form Fonts Questions Part 1

牧云@^-^@ 提交于 2019-12-18 06:48:25

问题


I have some user controls in a windows form. I wonder

  1. if i set the Font property of the main form, will its child get a) a copy of the new Font, or b) a reference to the new Font, or c) nothing?

  2. Does a font need to be disposed? For example, can I do the following code safely?

    form.Font = new Font(...);

  3. Will a font get disposed automatically when the parent (Form or UserControl) is disposed?

Thanks,
Gilbert


回答1:


  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.

  2. Yes. The code is fine, the Font property setter already disposes the previously assigned font.

  3. Yes, it is disposed by the UserControl. Which in turn is automatically disposed by its parent.




回答2:


  1. When Font property of the main form is set, its child controls get neither deep copy of System.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).
  2. Windows Forms does what is logically expected with any other property: When you assign a System.Drawing.Font object to Font property of a control, a previously assigned System.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.
  3. System.Drawing.Font object assigned to Font property of a control is NOT getting disposed when the control is disposed, or another System.Drawing.Font is assigned to Font property. So you are safe to create a single System.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:


  1. 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.
  2. The garbage collector should take care of it.
  3. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!