Windows Form Fonts Questions Part 1

后端 未结 4 914
小鲜肉
小鲜肉 2020-12-19 16:59

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

4条回答
  •  旧时难觅i
    2020-12-19 17:44

    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.

提交回复
热议问题