form1.vb

来自「Visual Basic .NET程序设计教程源代码」· VB 代码 · 共 135 行

VB
135
字号
Imports System.Drawing.Drawing2D

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    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 Button1 As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(208, 216)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(16, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(8, 8)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 245)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "GDI+特效演示"
        Me.ResumeLayout(False)

    End Sub

#End Region
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Hide()
        Dim g1 As Graphics
        Dim g2 As Graphics
        g1 = Me.CreateGraphics()
        g2 = PictureBox1.CreateGraphics

        '设置窗体背景
        Dim ImagePath As String
        ImagePath = "f:\vb.app\ch11\"
        Dim backImage As Image
        backImage = New Bitmap(ImagePath & "Globe.wmf")
        '绘制窗体背景
        g1.DrawImage(backImage, 0, 0)

        '演练纹理特性
        '设置画刷,利用材质作为画刷
        Dim textureImage As Image = New Bitmap(ImagePath & "3.jpg")
        Dim textureBrush = New TextureBrush(textureImage)

        '定义一个黑色的画笔
        Dim shadowBrush As Brush = New SolidBrush(Color.Black)

        '演练渐变填充特性,
        '定义渐变色画刷( )
        Dim linearGradBrush As Brush
        linearGradBrush = New LinearGradientBrush(New Point(0, 0), New Point(0, 15), Color.Blue, Color.Red)

        Dim fntFont As Font
        fntFont = New Font("黑体", 20, FontStyle.Italic Or FontStyle.Underline)
        '在窗体上绘制文字
        g1.DrawString("你好,GDI+!", fntFont, shadowBrush, 1, 31)
        g1.DrawString("你好,GDI+!", fntFont, textureBrush, 0, 30)
        g1.DrawString("你好,GDI+!", fntFont, linearGradBrush, 32, 62)

        '反走样
        Dim p As Pen = New Pen(Color.Red, 5)
        g1.SmoothingMode = SmoothingMode.AntiAlias
        g1.DrawString("反走样!", fntFont, linearGradBrush, 22, 182)
        g1.DrawEllipse(p, 0, 180, 150, 55)

        '旋转与平移文字
        Dim graphicState As GraphicsState
        graphicState = g1.Save()
        g1.RotateTransform(-10)
        g1.TranslateTransform(-45, 45)
        g1.DrawString("你好,GDI+!", fntFont, linearGradBrush, 22, 82)
        g1.Restore(graphicState)

        '利用alpha混合技术生成一个具有立体效果的图形
        Dim i As Integer
        For i = 1 To 10
            Dim newpen As New Pen(Color.FromArgb(i * 25, Color.Red), 5)
            g1.DrawRectangle(newpen, 150 + i * 5, 130 + i * 5, 80, 80)
        Next i

        '释放GDI+对象
        fntFont.Dispose()
        linearGradBrush.Dispose()
        shadowBrush.Dispose()
        textureBrush.Dispose()
        backImage.Dispose()
        g1.Dispose()
    End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?