实例说明
在本实例中,我们制作一个能够显示多种形式文本的应用程序。程序运行后,即在窗体上的不同区域输出不同的文字。程序运行结果如图57-1所示。
图57-1 运行结果
技术要点
l 设定不同的Brush和Font
l 输出字体
实现过程
■ 新建项目
打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中,选择"Windows应用程序",在名称域中输入"GdipText",然后选择保存路径。单击"确认"。
Public Sub New()
MyBase.New()
TextSample = Me
InitializeComponent()
serifFontFamily = New FontFamily(GenericFontFamilies.Serif)
Me.SetStyle(ControlStyles.Opaque,True)
Me.SetStyle(ControlStyles.ResizeRedraw,True)
Dim backgroundImage As Image
'设定背景图片
backgroundImage = New Bitmap(System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("colorbars.jpg"))
'新建一个画刷,我们将使用它在背景图片上画图
backgroundBrush = New TextureBrush(backgroundImage)
'设定文本图片
Dim textImage As Image = New Bitmap(System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("marble.jpg"))
textTextureBrush = New TextureBrush(textImage)
'设定要使用字体格式
Me.Font = New Font(serifFontFamily,20)
titleFont = New Font(serifFontFamily,60)
textFont = New Font(serifFontFamily,11)
'建立一个阴影画刷
titleShadowBrush = New SolidBrush(Color.FromArgb(70,Color.Black))
'用设定的字体和画刷输出Japanese文本
Try
japaneseFont = New Font("MS Mincho",36)
linearGradBrush = New LinearGradientBrush(New Point(0,0),New Point(0,45),Color.Blue,Color.Red)
Catch ex As Exception
MessageBox.Show("The Japanese font MS Mincho needs be present to run the Japanese part of this sample" & ControlChars.CrLf & "" & ControlChars.CrLf & "" + ex.Message)
doJapaneseSample = False
End Try
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
'用texture画刷填充背景,并应用到一个白画布上
g.FillRectangle(backgroundBrush,ClientRectangle)
g.FillRectangle(New SolidBrush(Color.FromArgb(180,Color.White)),ClientRectangle)
g.DrawString("欢迎大家来到VB.NET的世界!",Me.Font,New SolidBrush(Color.Black),10,10)
Dim titleText As String = "图形示例"
g.DrawString(titleText,titleFont,titleShadowBrush,15,25)
g.DrawString(titleText,textTextureBrush,20)
Dim textToDraw As String = "银河文化公司"
Dim windowCenter As Double = Me.DisplayRectangle.Width / 2
Dim stringSize As SizeF = g.MeasureString(textToDraw,textFont)
Dim startPos As Double = windowCenter - (stringSize.Width / 2)
g.DrawString(textToDraw,textFont,New SolidBrush(Color.Red),CType(startPos,Single),10)
Dim rectangle1 As RectangleF = New RectangleF(20,150,250,300)
g.FillRectangle(New SolidBrush(Color.Gainsboro),rectangle1)
g.DrawString(flowedText1,New SolidBrush(Color.Blue),rectangle1)
Dim rectangle2 As RectangleF = New RectangleF(450,rectangle2)
Dim format As StringFormat = New StringFormat()
format.Alignment = StringAlignment.Center
g.DrawString(flowedText2,rectangle2,Format)
Dim characters As Integer = 0
Dim lines As Integer = 0
g.MeasureString(flowedText2,rectangle2.Size,format,characters,lines)
Dim whatRenderedText As String = "共输出了" + CType(characters,String) + " 字符和 " + CType(lines,String) + "行"
g.DrawString(whatRenderedText,400,440)
'旋转刚才输出的Japanese文本
If (doJapaneseSample) Then
g.RotateTransform(-30)
g.TranslateTransform(-180,300)
g.DrawString(japaneseText,japaneseFont,linearGradBrush,200,140)
g.ResetTransform()
End If
End Sub
■ 运行程序
单击菜单"调试|启动"或单击 图标运行程序。
小结
本实例使用不同的画刷(Brush)和字体(Font),在不同的区域输出不同的文字,并可以旋转文字,而并不需要使用API函数。
原文链接:https://www.f2er.com/vb/264329.html