VB.NET:如何在运行时编写字体并将字体应用于标签? [英] VB.NET: How to compose and apply a font to a label in runtime?

查看:133
本文介绍了VB.NET:如何在运行时编写字体并将字体应用于标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2008在Visual Basic .NET中开发Windows窗体应用程序.

I'm developing a Windows Forms Application in Visual Basic .NET with Visual Studio 2008.

我正在尝试根据用户首选项在运行时编写字体(家族名称,字体大小和样式),并将其应用于标签.

I'm trying to compose fonts (Family name, font size, and the styles) at runtime, based on user preferences, and apply them to labels.

为了简化用户界面,并考虑到一台以上需要使用相同字体的计算机之间的兼容性,我将使用 InstalledFontCollection ,但是一组按钮,将设置一些选定的字体,我知道这些字体会出现在所有机器上(诸如Verdana之类的字体).

For the sake of both a simplier user interface, and compatibility between more than one machine requiring to use the same font, I'll NOT use the InstalledFontCollection, but a set of buttons that will set few selected fonts, that I know to be present in all machines (fonts like Verdana).

因此,我必须在将创建字体的模块上创建一个Public Sub,但是我不知道该如何编码.还有四个设置样式的CheckBox,分别是粗体,斜体,下划线和删除线.

So, I have to make a Public Sub on a Module that will create fonts, but I don't know how to code that. There are also four CheckBoxes that set the styles, Bold, Italic, Underline and Strikeout.

我应该如何编码? SomeLabel.Font.Bold 属性为只读属性,将"Times New Roman"之类的字符串转换为FontFamily类型时似乎出现了问题. (它只是说它做不到)

How should I code this? The SomeLabel.Font.Bold property is readonly, and there seems to be a problem when converting a string like "Times New Roman" to a FontFamily type. (It just says it could not do it)

喜欢

Dim NewFontFamily As FontFamily = "Times New Roman"

谢谢.

推荐答案

这应该可以解决字体问题:

This should resolve your font issue:

Label1.Font = New Drawing.Font("Times New Roman", _
                               16,  _
                               FontStyle.Bold or FontStyle.Italic)

有关此处Font属性的MSDN文档

创建此字体的函数的可能实现如下所示:

A possible implementation for the function that creates this font might look like this:

Public Function CreateFont(ByVal fontName As String, _
                           ByVal fontSize As Integer, _
                           ByVal isBold As Boolean, _
                           ByVal isItalic As Boolean, _
                           ByVal isStrikeout As Boolean) As Drawing.Font

    Dim styles As FontStyle = FontStyle.Regular

    If (isBold) Then
        styles = styles Or FontStyle.Bold
    End If

    If (isItalic) Then
        styles = styles Or FontStyle.Italic
    End If

    If (isStrikeout) Then
        styles = styles Or FontStyle.Strikeout
    End If

    Dim newFont As New Drawing.Font(fontName, fontSize, styles)
    Return newFont

End Function

字体是不可变的,这意味着一旦创建字体,便无法对其进行更新.因此,您注意到的所有只读属性.

Fonts are immutable, that means once they are created they cannot be updated. Therefore all the read-only properties that you have noticed.

这篇关于VB.NET:如何在运行时编写字体并将字体应用于标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆