笔记6:字体



字体对象。有属性:字型、字体大小、字体样式等。

重要:创建的字体对象后,其属性为只读。因此创建时须考虑好。


FontFamily 字体家庭成员

这是什么意思?

把字体相似的一些字体归纳到一个大家庭中,即集合中FontFamilies。

那么其中的每一个字体就是其中的成员。

为什么产生这个?

当程序传到另一处时,别处无对应字体时,就应用另一个家庭成员的字体来代替,故又称后备字体。

例如:网页设计好后,传到用户处,但用户处无对应字体,一般这时会用宋体之类的来代替。


注意:FontFamily的构造函数,只取字体来设置,不能对其设置样式、大小,比如不能设置其大小。






字体的样式

有时为了简化,可以设置其中一个基准的fontfamily,然后真正使用的字体来具体创建大小和样式:

(因为字体对象一创建后不能更改属性,所以不能偷懒通过属性来更改大小 之类的)


Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fnf As New FontFamily("方正启体简体")
        Dim gr As Graphics = Me.CreateGraphics
        Dim br As New SolidBrush(Color.Crimson)

        Dim fn(3) As Font
        fn(0) = New Font(fnf, 14, FontStyle.Bold)
        fn(1) = New Font(fnf, 14, FontStyle.Italic)
        fn(2) = New Font(fnf, 14, FontStyle.Strikeout)
        fn(3) = New Font(fnf, 14, FontStyle.Underline)

        For i As Int32 = 0 To 3
            gr.DrawString("云地闪", fn(i), Brushes.Brown, 10, 10 + i * 23)
        Next
    End Sub
End Class






字体的属性

看一下属性:


Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fn As New Font("宋体", 0.2, FontStyle.Bold, GraphicsUnit.Inch)
        Dim gr As Graphics = Me.CreateGraphics
        Dim br As New SolidBrush(Color.Crimson)

        'Height是像素单位(上面设置是0.2inch,输出是22像素)
        Dim s As String = "字体名:" & fn.Name & vbCrLf _
                         & "字体大小:" & fn.Size & vbCrLf _
                         & "字体间距:" & fn.Height & vbCrLf _
                         & "字体样式:" & fn.Style.ToString & vbCrLf

        gr.DrawString(s, fn, Brushes.Brown, 10, 10)
    End Sub
End Class






字体对象的方法:


Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fn As New Font("宋体", 11, FontStyle.Italic)
        Dim cfn As Font = fn.Clone     '克隆,创建副本
        Dim gr As Graphics = Me.CreateGraphics

        gr.DrawString(cfn.Equals(fn), cfn, Brushes.BurlyWood, 10, 10)
        gr.DrawString(fn.GetHashCode, cfn, Brushes.Black, 10, 25) '哈希值验证两字体数值一致
        gr.DrawString(cfn.GetHashCode, cfn, Brushes.CadetBlue, 10, 40)
        gr.DrawString(cfn.GetHeight, cfn, Brushes.BurlyWood, 10, 55) '属性中Height是该值取整
        gr.DrawString(cfn.ToHfont, cfn, Brushes.Chocolate, 10, 70) '字体句柄
        gr.DrawString(cfn.ToString, cfn, Brushes.Red, 10, 85)
    End Sub
End Class








字体透明

即,使用颜色时,用透明颜色fromArgb

formArgb(int32,color)  第一个int32就是0-255表明透明度,255不透明,0完全透明。

Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim br1 As New SolidBrush(Color.Red)
        Dim col As Color = Color.FromArgb(100, Color.Red)
        Dim br2 As New SolidBrush(col)
        Dim gr As Graphics = PictureBox1.CreateGraphics
        Dim fn As New Font("宋体", 60, FontStyle.Bold)

        gr.DrawString("云地闪", fn, br1, 0, 0)
        gr.DrawString("云地闪", fn, br2, 0, 0 + fn.Height) '透明字体
        gr.DrawString("指定矩形内输出文本", fn, br2, New Rectangle(0, 2 * fn.Height, 200, 300)) '在指定矩形区域输出文字
    End Sub
End Class

注意:一般都是从左到右输出。可以用rectangle来指定输出的文字的区域,这样,文字就自动换行输出。












文字渐变与透明

下面弄一个渐变的文字效果及叠加透明


为了看清是因为笔刷brush的原因造成,下面定义时放在一个语句中,所以brush的定义有点长:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim br1 As New LinearGradientBrush(New Point(0, 0), New Point(150, 150), Color.Red, Color.Blue)
        Dim br2 As New LinearGradientBrush(New Point(0, 150), New Point(150, 300), Color.FromArgb(150, Color.Red), Color.FromArgb(150, Color.Blue))
        Dim gr As Graphics = PictureBox1.CreateGraphics
        Dim fn As New Font("宋体", 60, FontStyle.Bold)


        gr.DrawString("找对象", fn, br1, 0, 0) '渐变色文字
        gr.DrawString("找对象", fn, br2, 0, 150) '渐变色透明文字
    End Sub
End Class
注意: 再次点击button1时,会在原位置叠加,多次点击,透明效果就失效了:)









文字的对齐

文字对齐有水平的左、中、右;以及垂直的上、中、下对齐。

都是通过StringFromat的属性FormatFlag这个标志来设置。


Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim br As New LinearGradientBrush(New Point(0, 0), New Point(200, 230), Color.White, Color.Green)
        Dim fn As New Font("楷体", 12, FontStyle.Bold)
        Dim gr As Graphics = PictureBox1.CreateGraphics
        Dim sf As New Drawing.StringFormat
        Dim sText As String = "文件夹是可以在其中存储文件的容器。如果在桌面上放置数以千计的纸质文件," _
                              & "要在需要时查找某个特定文件几乎是不可能的。这就是人们时常把纸质文件存" _
                              & "储在文件柜内文件夹中的原因。"

        sf.Alignment = StringAlignment.Far '水平对齐:以左上角为基准,near居左,far居中,center居中
        sf.LineAlignment = StringAlignment.Center '垂直对齐:near居上,far居下,center居中
        gr.DrawString(sText, fn, br, New Rectangle(10, 10, 200, 230), sf) '最后参数指明显示模式
        gr.DrawRectangle(Pens.White, New Rectangle(10, 10, 200, 230)) '矩形
    End Sub
End Class








文字的垂直输出


'垂直对齐

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim sf As New StringFormat

        sf.FormatFlags = StringFormatFlags.DirectionVertical '垂直对齐
        gr.DrawString("垂直效果文字", New Font("宋体", 14), Brushes.Red, New Point(10, 10), sf)
    End Sub
End Class






文字的制表位(按表格方式输出)

主要有两个:1、ControlChars集合中控制符的输出

                        2、StringFormat有方法SetTabStop,来设置一个数组(含有每列的间距)


Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim s As String = "Name" & ControlChars.Tab & "Test1" & ControlChars.Tab & "Test2" _
                         & ControlChars.Tab & "Test3" & Chr(10)
        s &= "Joe" & ControlChars.Tab & "99" & ControlChars.Tab & "88" & ControlChars.Tab & "91" & ControlChars.Tab & Chr(10)
        s &= "Mary" & ControlChars.Tab & "78" & ControlChars.Tab & "66" & ControlChars.Tab & "33" & ControlChars.Tab & Chr(10)
        s &= "Sam" & ControlChars.Tab & "65" & ControlChars.Tab & "44" & ControlChars.Tab & "66" & ControlChars.Tab & Chr(10)
        s &= "Tom" & ControlChars.Tab & "87" & ControlChars.Tab & "45" & ControlChars.Tab & "22" & ControlChars.Tab & Chr(10)


        Dim gr As Graphics = PictureBox1.CreateGraphics
        Dim fn As New Font("楷体", 12, FontStyle.Regular)
        Dim br As New SolidBrush(Color.Red)
        Dim sf As New StringFormat
        Dim tabs() As Single = {150, 50, 100, 100}


        sf.SetTabStops(0, tabs) '设置制表位(间隔,如图所示)
        gr.DrawString(s, fn, br, New Rectangle(10, 10, 455, 100), sf)
    End Sub
End Class






文字的修正(抗锯齿)

文字变大后,其中的锯齿就显得非常明显。可以通过System.Drawing.Text.TextRenderingHint中,专门对文字效果的修正。

可以看到放大后:弯曲及斜线部分的锯齿情况:


'抗锯齿等效果
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text.TextRenderingHint '文本质量控制
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics

        gr.DrawString("地", New Font("宋体", 100, FontStyle.Bold), Brushes.Red, New Point(10, 10))

        gr.TextRenderingHint = AntiAlias
        'gr.TextRenderingHint = AntiAliasGridFit
        'gr.TextRenderingHint = ClearTypeGridFit
        'gr.TextRenderingHint = SingleBitPerPixelGridFit
        gr.DrawString("地", New Font("宋体", 100, FontStyle.Bold), Brushes.Red, New Point(150, 10))

        gr.TextRenderingHint = SingleBitPerPixel
        gr.DrawString("地", New Font("宋体", 100, FontStyle.Bold), Brushes.Red, New Point(300, 10))
    End Sub
End Class






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值