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

📄 mybutton.vb

📁 编程之道VB.NETt程序设计入门-589M.zip
💻 VB
字号:
Imports System.Drawing
Imports System.Windows.Forms

Public Class GradientButton
    Inherits Button

    Private m_startColor, m_endColor As Color
    Private m_textFormat As New StringFormat()

    Public Sub New()
        MyBase.New()
        m_startColor = Color.Aqua
        m_endColor = Color.Yellow
        m_textFormat.Alignment = StringAlignment.Center
        m_textFormat.LineAlignment = StringAlignment.Center
    End Sub

    Public Property StartColor() As Color
        Get
            Return m_startColor
        End Get
        Set(ByVal Value As Color)
            m_startColor = Value
        End Set
    End Property

    Public Property EndColor() As Color
        Get
            Return m_endColor
        End Get
        Set(ByVal Value As Color)
            m_endColor = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
        MyBase.OnPaint(pe)
        Dim g As Graphics = pe.Graphics
        Dim clientRect As Rectangle = pe.ClipRectangle
        clientRect.Inflate(-1, -1)

        Dim bgBrush As Brush = New Drawing2D.LinearGradientBrush( _
                                    New Point(clientRect.X, clientRect.Y), _
                                    New Point(clientRect.Width, clientRect.Height), StartColor, EndColor)

        g.FillRectangle(bgBrush, clientRect)
        Dim clientRectF As New Drawing.RectangleF(clientRect.X, clientRect.Y, _
                                                clientRect.Width, clientRect.Height)
        g.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), clientRectF, m_textFormat)

    End Sub

    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        MyBase.OnClick(e)
        Dim r As New Random()

        StartColor = Color.FromArgb(r.Next())
        EndColor = Color.FromArgb(r.Next())
    End Sub

End Class

⌨️ 快捷键说明

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