⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Imports System.Drawing.Drawing2D
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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
                Friend WithEvents bttnRectangleGradient As System.Windows.Forms.Button
                Friend WithEvents bttnDiagGradient As System.Windows.Forms.Button
    Friend WithEvents bttnLinearGradient As System.Windows.Forms.Button
    Friend WithEvents bttnGradientText As System.Windows.Forms.Button
    Friend WithEvents bttnPathGradient As System.Windows.Forms.Button
    Friend WithEvents bttnAnimatedGradient As System.Windows.Forms.Button
    
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.bttnDiagGradient = New System.Windows.Forms.Button()
        Me.bttnLinearGradient = New System.Windows.Forms.Button()
        Me.bttnGradientText = New System.Windows.Forms.Button()
        Me.bttnRectangleGradient = New System.Windows.Forms.Button()
        Me.bttnAnimatedGradient = New System.Windows.Forms.Button()
        Me.bttnPathGradient = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'bttnDiagGradient
        '
        Me.bttnDiagGradient.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnDiagGradient.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnDiagGradient.Location = New System.Drawing.Point(8, 352)
        Me.bttnDiagGradient.Name = "bttnDiagGradient"
        Me.bttnDiagGradient.Size = New System.Drawing.Size(192, 24)
        Me.bttnDiagGradient.TabIndex = 4
        Me.bttnDiagGradient.Text = "Diagonal Linear Gradient"
        '
        'bttnLinearGradient
        '
        Me.bttnLinearGradient.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnLinearGradient.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnLinearGradient.Location = New System.Drawing.Point(8, 320)
        Me.bttnLinearGradient.Name = "bttnLinearGradient"
        Me.bttnLinearGradient.Size = New System.Drawing.Size(192, 24)
        Me.bttnLinearGradient.TabIndex = 6
        Me.bttnLinearGradient.Text = "Linear Gradient"
        '
        'bttnGradientText
        '
        Me.bttnGradientText.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnGradientText.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnGradientText.Location = New System.Drawing.Point(224, 320)
        Me.bttnGradientText.Name = "bttnGradientText"
        Me.bttnGradientText.Size = New System.Drawing.Size(192, 24)
        Me.bttnGradientText.TabIndex = 0
        Me.bttnGradientText.Text = "Gradient Text"
        '
        'bttnRectangleGradient
        '
        Me.bttnRectangleGradient.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnRectangleGradient.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnRectangleGradient.Location = New System.Drawing.Point(440, 320)
        Me.bttnRectangleGradient.Name = "bttnRectangleGradient"
        Me.bttnRectangleGradient.Size = New System.Drawing.Size(192, 24)
        Me.bttnRectangleGradient.TabIndex = 3
        Me.bttnRectangleGradient.Text = "Rectangle Gradient"
        '
        'bttnAnimatedGradient
        '
        Me.bttnAnimatedGradient.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnAnimatedGradient.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnAnimatedGradient.Location = New System.Drawing.Point(440, 352)
        Me.bttnAnimatedGradient.Name = "bttnAnimatedGradient"
        Me.bttnAnimatedGradient.Size = New System.Drawing.Size(192, 24)
        Me.bttnAnimatedGradient.TabIndex = 2
        Me.bttnAnimatedGradient.Text = "Animated Gradient"
        '
        'bttnPathGradient
        '
        Me.bttnPathGradient.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.bttnPathGradient.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnPathGradient.Location = New System.Drawing.Point(224, 352)
        Me.bttnPathGradient.Name = "bttnPathGradient"
        Me.bttnPathGradient.Size = New System.Drawing.Size(192, 24)
        Me.bttnPathGradient.TabIndex = 1
        Me.bttnPathGradient.Text = "Path Gradient"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(640, 381)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnLinearGradient, Me.bttnDiagGradient, Me.bttnRectangleGradient, Me.bttnAnimatedGradient, Me.bttnPathGradient, Me.bttnGradientText})
        Me.Name = "Form1"
        Me.Text = "GDI+ Gradients"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub bttnGradientText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnGradientText.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
        Dim largeFont As New Font("Comic Sans MS", 48, FontStyle.Bold, GraphicsUnit.Point)
        Dim gradientStart As New PointF(0, 0)
        Dim txt As String = "Gradient Text"
        Dim txtSize As New SizeF()
        txtSize = G.MeasureString(txt, largeFont)
        Dim gradientEnd As New PointF()
        gradientEnd.X = txtSize.Width
        gradientEnd.Y = txtSize.Height
        Dim grBrush As New LinearGradientBrush(gradientStart, _
                        gradientEnd, Color.Yellow, Color.Blue)
        G.DrawString(txt, largeFont, grBrush, 20, 20)
    End Sub

    Private Sub bttnPathGradient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPathGradient.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        Dim path As New System.Drawing.Drawing2D.GraphicsPath()
        path.AddLine(New Point(10, 10), New Point(400, 10))
        path.AddLine(New Point(400, 10), New Point(400, 250))
        path.AddLine(New Point(400, 250), New Point(10, 250))
        Dim pathBrush As New System.Drawing.Drawing2D.PathGradientBrush(path)
        Dim centerColor As Color = Color.Red
        Dim surroundColors() As Color = {Color.Yellow, Color.Green, Color.Blue, Color.Cyan}

        pathBrush.CenterColor = centerColor
        pathBrush.SurroundColors = surroundColors

        G.FillPath(pathBrush, path)
    End Sub

    Private Sub bttnAnimatedGradient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnAnimatedGradient.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        Dim path As New System.Drawing.Drawing2D.GraphicsPath()
        path.AddRectangle(New Rectangle(10, 10, 400, 100))

        Dim pathBrush As New System.Drawing.Drawing2D.PathGradientBrush(path)
        Dim centerColor As Color = Color.Silver
        Dim surroundColors() As Color = {Color.Black, Color.Black, Color.Black, Color.Black}
        Dim colorCount As Integer = 1

        pathBrush.CenterColor = centerColor
        pathBrush.SurroundColors = surroundColors
        Dim i As Double
        For i = 0 To 100
            pathBrush.CenterPoint = New PointF(200, i)
            G.FillPath(pathBrush, path)
        Next

        For i = 0 To 400
            pathBrush.CenterPoint = New PointF(i, 50)
            G.FillPath(pathBrush, path)
        Next

    End Sub

    Private Sub bttnRectangleGradient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnRectangleGradient.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        Dim path As New System.Drawing.Drawing2D.GraphicsPath()
        path.AddRectangle(New Rectangle(20, 20, 400, 200))
        Dim pathBrush As New System.Drawing.Drawing2D.PathGradientBrush(path)
        Dim centerColor As Color = Color.LightYellow
        Dim surroundColors() As Color = {Color.SteelBlue}
        pathBrush.CenterColor = centerColor
        pathBrush.SurroundColors = surroundColors
        G.FillPath(pathBrush, path)
    End Sub

    Private Sub bttnLinearGradient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDiagGradient.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        Dim path As New System.Drawing.Drawing2D.GraphicsPath()
        path.AddRectangle(New Rectangle(20, 20, 400, 200))
        Dim lBrush As New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(20, 20, 400, 200), Color.Beige, Color.Purple, System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal)
        G.FillPath(lBrush, path)
    End Sub


    Private Sub LinearGradient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnLinearGradient.Click
        Dim G As Graphics
        G = Me.CreateGraphics
        G.Clear(Me.BackColor)
        Dim R As New RectangleF(20, 20, 300, 100)
        Dim startColor As Color = Color.BlueViolet
        Dim EndColor As Color = Color.LightYellow
        Dim LGBrush As New System.Drawing.Drawing2D.LinearGradientBrush(R, startColor, EndColor, LinearGradientMode.Horizontal)
        G.FillRectangle(LGBrush, New Rectangle(20, 20, 200, 100))
        G.FillRectangle(LGBrush, New Rectangle(20, 150, 600, 100))
    End Sub
End Class

⌨️ 快捷键说明

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