How to change font size without creating a new font

帅比萌擦擦* 提交于 2021-02-07 18:46:28

问题


Is it possible to change the size of a font in .net winforms without having to create a new Font with the new size?


回答1:


No. Font size is readonly for existing "Font" objects.




回答2:


You could do something like this with an Extension method.

Imports System.Runtime.CompilerServices

Module FontExtensions

<Extension()> Public Function ToSize(ByVal OriginalFont As Font, ByVal NewSize As Single) As Font

        Dim NewFont As Font

        NewFont = New Font(OriginalFont.FontFamily, NewSize, OriginalFont.Style)

        Return NewFont

    End Function

End Module

and then call it like this...

SomeObject.Font = Font.ToSize(12)

It's still creating a new font behind the scenes, but your application code is not cluttered with the creation process.




回答3:


Make sure you use the constructor method that allows you to use the base font and pass in the new size you desire. This will save you some code from the other approaches.



来源:https://stackoverflow.com/questions/922761/how-to-change-font-size-without-creating-a-new-font

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