[VB.NET]多彩文本

前端之家收集整理的这篇文章主要介绍了[VB.NET]多彩文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
多彩文本 @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 Studio.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@MessageBox.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函数

猜你在找的VB相关文章