我允许在我的应用程序中使用主题。所以我根据用户选择的主题创建了一种字体。但是,我需要将fontstyle从常规更改为粗体,但我不想重新创建字体。
我这样做,因为并非所有用户都会在他们的机器上安装字体。所以我将字体嵌入到应用程序中。
例如:我可能会为文本框分配如下字体:
txtbox.font = theme_font
有没有办法简单地将样式更改为粗体?
txtbox.font = theme_font.fontstyle.bold ' <-- this doesn't work
我这样调用字体创建子程序:
Public Shared theme_font = BerlinSans.GetInstance(theme_font_size, FontStyle.Regular)
这是被调用的子程序:
Module BerlinSans
'PRIVATE FONT COLLECTION TO HOLD THE DYNAMIC FONT
Private _pfc As PrivateFontCollection = Nothing
Public ReadOnly Property GetInstance(ByVal Size As Single, ByVal style As FontStyle) As Font
Get
'IF THIS IS THE FIRST TIME GETTING AN INSTANCE
'LOAD THE FONT FROM RESOURCES
If _pfc Is Nothing Then LoadFont()
'RETURN A NEW FONT OBJECT BASED ON THE SIZE AND STYLE PASSED IN
Return New Font(_pfc.Families(0), Size, style)
End Get
End Property
Private Sub LoadFont()
Try
'INIT THE FONT COLLECTION
_pfc = New PrivateFontCollection
'LOAD MEMORY POINTER FOR FONT RESOURCE
Dim fontMemPointer As IntPtr = Marshal.AllocCoTaskMem(My.Resources.BRLNSR.Length)
'COPY THE DATA TO THE MEMORY LOCATION
Marshal.Copy(My.Resources.BRLNSR, 0, fontMemPointer, My.Resources.BRLNSR.Length)
'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
_pfc.AddMemoryFont(fontMemPointer, My.Resources.BRLNSR.Length)
'FREE UNSAFE MEMORY
Marshal.FreeCoTaskMem(fontMemPointer)
Catch ex As Exception
'ERROR LOADING FONT. HANDLE EXCEPTION HERE
End Try
End Sub
End Module
答案 0 :(得分:0)
您可以使用Font(Font, FontStyle)
constructor。只需将旧字体和您想要使用的新样式传递给它,它就会将旧字体中的属性复制到新字体中(当然除了样式)。
txtbox.Font = New Font(theme_font, FontStyle.Bold)