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