📄 form1.vb
字号:
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents txtSampleText As System.Windows.Forms.TextBox
Friend WithEvents nudFontSize As System.Windows.Forms.NumericUpDown
Friend WithEvents btnShadow As System.Windows.Forms.Button
Friend WithEvents btnEmboss As System.Windows.Forms.Button
Friend WithEvents btnReflect As System.Windows.Forms.Button
Friend WithEvents btnTransform As System.Windows.Forms.Button
Friend WithEvents btn3D As System.Windows.Forms.Button
Friend WithEvents picCanvas As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.picCanvas = New System.Windows.Forms.PictureBox()
Me.txtSampleText = New System.Windows.Forms.TextBox()
Me.nudFontSize = New System.Windows.Forms.NumericUpDown()
Me.btnShadow = New System.Windows.Forms.Button()
Me.btnEmboss = New System.Windows.Forms.Button()
Me.btnReflect = New System.Windows.Forms.Button()
Me.btnTransform = New System.Windows.Forms.Button()
Me.btn3D = New System.Windows.Forms.Button()
CType(Me.nudFontSize, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'picCanvas
'
Me.picCanvas.BackColor = System.Drawing.Color.White
Me.picCanvas.Location = New System.Drawing.Point(200, 8)
Me.picCanvas.Name = "picCanvas"
Me.picCanvas.Size = New System.Drawing.Size(328, 288)
Me.picCanvas.TabIndex = 0
Me.picCanvas.TabStop = False
'
'txtSampleText
'
Me.txtSampleText.Location = New System.Drawing.Point(16, 8)
Me.txtSampleText.Name = "txtSampleText"
Me.txtSampleText.Size = New System.Drawing.Size(144, 21)
Me.txtSampleText.TabIndex = 1
Me.txtSampleText.Text = "示例文字"
'
'nudFontSize
'
Me.nudFontSize.Location = New System.Drawing.Point(16, 40)
Me.nudFontSize.Name = "nudFontSize"
Me.nudFontSize.Size = New System.Drawing.Size(72, 21)
Me.nudFontSize.TabIndex = 2
Me.nudFontSize.Value = New Decimal(New Integer() {50, 0, 0, 0})
'
'btnShadow
'
Me.btnShadow.Location = New System.Drawing.Point(16, 96)
Me.btnShadow.Name = "btnShadow"
Me.btnShadow.Size = New System.Drawing.Size(112, 24)
Me.btnShadow.TabIndex = 3
Me.btnShadow.Text = "阴影效果"
'
'btnEmboss
'
Me.btnEmboss.Location = New System.Drawing.Point(16, 136)
Me.btnEmboss.Name = "btnEmboss"
Me.btnEmboss.Size = New System.Drawing.Size(112, 23)
Me.btnEmboss.TabIndex = 4
Me.btnEmboss.Text = "浮雕效果"
'
'btnReflect
'
Me.btnReflect.Location = New System.Drawing.Point(16, 176)
Me.btnReflect.Name = "btnReflect"
Me.btnReflect.Size = New System.Drawing.Size(112, 23)
Me.btnReflect.TabIndex = 4
Me.btnReflect.Text = "倒影效果"
'
'btnTransform
'
Me.btnTransform.Location = New System.Drawing.Point(16, 216)
Me.btnTransform.Name = "btnTransform"
Me.btnTransform.Size = New System.Drawing.Size(112, 23)
Me.btnTransform.TabIndex = 4
Me.btnTransform.Text = "变形效果"
'
'btn3D
'
Me.btn3D.Location = New System.Drawing.Point(16, 256)
Me.btn3D.Name = "btn3D"
Me.btn3D.Size = New System.Drawing.Size(112, 23)
Me.btn3D.TabIndex = 4
Me.btn3D.Text = "三维效果"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(536, 302)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnEmboss, Me.btnShadow, Me.nudFontSize, Me.txtSampleText, Me.picCanvas, Me.btnReflect, Me.btnTransform, Me.btn3D})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.nudFontSize, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnShadow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShadow.Click
Dim textSize As SizeF
Dim g As Graphics
Dim shadowBrush As Brush = Brushes.LightGray
Dim textBrush As Brush = Brushes.Black
Dim textFont As New Font("宋体", Me.nudFontSize.Value, FontStyle.Regular)
Dim x, y As Single
' 在图片框picCanvas上面建立一个新的空白Graphics
g = picCanvas.CreateGraphics()
g.Clear(Color.White)
' 得到示例文字的大小
textSize = g.MeasureString(Me.txtSampleText.Text, textFont)
' 得到应该放置示例文字的位置
x = (picCanvas.Width - textSize.Width) / 2
y = (picCanvas.Height - textSize.Height) / 2
' 画出阴影
g.DrawString(txtSampleText.Text, textFont, shadowBrush, _
x + 10, y + 10)
' 画出文字
g.DrawString(txtSampleText.Text, textFont, textBrush, x, y)
End Sub
Private Sub btnEmboss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmboss.Click
Dim textSize As SizeF
Dim g As Graphics
Dim backBrush As Brush = Brushes.Black
Dim textBrush As Brush = Brushes.White
Dim textFont As New Font("宋体", Me.nudFontSize.Value, FontStyle.Regular)
Dim x, y As Single
' 在图片框picCanvas上面建立一个新的空白Graphics
g = picCanvas.CreateGraphics()
g.Clear(Color.White)
' 得到示例文字的大小
textSize = g.MeasureString(Me.txtSampleText.Text, textFont)
' 得到应该放置示例文字的位置
x = (picCanvas.Width - textSize.Width) / 2
y = (picCanvas.Height - textSize.Height) / 2
' 画出浮雕背景
g.DrawString(txtSampleText.Text, textFont, backBrush, x - 1, y - 1)
' 画出文字
g.DrawString(txtSampleText.Text, textFont, textBrush, x, y)
End Sub
Private Sub btnReflect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReflect.Click
Dim textSize As SizeF
Dim g As Graphics
Dim backBrush As Brush = Brushes.LightGray
Dim textBrush As Brush = Brushes.Black
Dim textFont As New Font("宋体", Me.nudFontSize.Value, FontStyle.Regular)
Dim x, y As Single
Dim textHeight As Single
' 在图片框picCanvas上面建立一个新的空白Graphics
g = picCanvas.CreateGraphics()
g.Clear(Color.White)
' 得到示例文字的大小
textSize = g.MeasureString(Me.txtSampleText.Text, textFont)
' 得到应该放置示例文字的位置
x = (picCanvas.Width - textSize.Width) / 2
y = (picCanvas.Height - textSize.Height) / 2
'得到文字高度
textHeight = textFont.GetHeight(g)
'将全局变换平移(x, y),也就是使画布上将要画的所有内容向左边移动x,向下移动y
g.TranslateTransform(x, y)
g.DrawString(txtSampleText.Text, textFont, textBrush, 0, -textHeight)
'将全局变量的y分量乘以-1,也就是使画布上将要画的所有内容垂直翻转180度
g.ScaleTransform(1, -1.0F)
g.DrawString(txtSampleText.Text, textFont, backBrush, 0, -textHeight)
End Sub
Private Sub btnTransform_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransform.Click
Dim textSize As SizeF
Dim g As Graphics
Dim textBrush As Brush = Brushes.Black
Dim textFont As New Font("宋体", Me.nudFontSize.Value, FontStyle.Regular)
Dim x, y As Single
' 在图片框picCanvas上面建立一个新的空白Graphics
g = picCanvas.CreateGraphics()
g.Clear(Color.White)
' 得到示例文字的大小
textSize = g.MeasureString(Me.txtSampleText.Text, textFont)
' 得到应该放置示例文字的位置
x = (picCanvas.Width - textSize.Width) / 2
y = (picCanvas.Height - textSize.Height) / 2
'将全局变换平移(x, y),也就是使画布上将要画的所有内容向左边移动x,向下移动y
g.TranslateTransform(x, y)
'做切变,将原始矩形的下边缘水平移动矩形高度的1.0倍
Dim textTransform As Matrix = g.Transform
textTransform.Shear(1.0, 0)
g.Transform = textTransform
' 画出文字
g.DrawString(txtSampleText.Text, textFont, textBrush, 0, 0)
End Sub
Private Sub btn3D_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3D.Click
Dim textSize As SizeF
Dim g As Graphics
Dim backBrush As Brush = Brushes.Gray
Dim textBrush As Brush = Brushes.Black
Dim textFont As New Font("宋体", Me.nudFontSize.Value, FontStyle.Regular)
Dim x, y As Single
' 在图片框picCanvas上面建立一个新的空白Graphics
g = picCanvas.CreateGraphics()
g.Clear(Color.White)
' 得到示例文字的大小
textSize = g.MeasureString(Me.txtSampleText.Text, textFont)
' 得到应该放置示例文字的位置
x = (picCanvas.Width - textSize.Width) / 2
y = (picCanvas.Height - textSize.Height) / 2
'画出背景的3D部分
Dim i As Integer
For i = 10 To 0 Step -1
g.DrawString(txtSampleText.Text, textFont, backBrush, _
x - i, y + i)
Next
'画出文字
g.DrawString(txtSampleText.Text, textFont, textBrush, x, y)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -