【问题标题】:Rounded Column in custom Button Control自定义按钮控件中的圆角柱
【发布时间】:2020-02-04 09:19:17
【问题描述】:

我正在尝试为自定义按钮控件创建一个类,该类将具有 2 个带圆角的渐变顶部和底部颜色。 下面是在顶部和底部为我提供两种渐变颜色的代码。但是,我遇到了一些问题,鼠标悬停属性不起作用并且图像也没有显示(因为新成分颜色与按钮文本和隐藏在渐变颜色后面的图像重叠)

有人可以帮助我使用这个控件吗?除了渐变颜色和圆角之外,所有控件都应该像使用 Windows 按钮控件一样工作吗?

如果您需要任何其他信息,请告诉我。

提前致谢。

Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Class MyButton
    Inherits Button

    Private m_TopColor As Color = Color.LightGreen
    Private m_BottomColor As Color = Color.Orange

    Public Property TopColor As Color
        Get
            Return m_TopColor
        End Get
        Set(ByVal value As Color)
            m_TopColor = value
            Me.Invalidate()
        End Set
    End Property

    Public Property BottomColor As Color
        Get
            Return m_BottomColor
        End Get
        Set(ByVal value As Color)
            m_BottomColor = value
            Me.Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Using lgb As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, m_TopColor, m_BottomColor, 90.0F)
            Using textBrush As SolidBrush = New SolidBrush(Me.ForeColor)
                Using format As StringFormat = New StringFormat()
                    format.Alignment = GetHorizontalAlignment()
                    format.LineAlignment = GetVerticalAlignment()
                    e.Graphics.FillRectangle(lgb, Me.ClientRectangle)
                    e.Graphics.DrawString(Me.Text, Me.Font, textBrush, Me.ClientRectangle, format)
                End Using
            End Using
        End Using
    End Sub

    Private Function GetVerticalAlignment() As StringAlignment
        Return CType(Math.Log(Me.TextAlign, 2D) / 4, StringAlignment)
    End Function

    Private Function GetHorizontalAlignment() As StringAlignment
        Return CType(Math.Log(Me.TextAlign, 2D) Mod 4, StringAlignment)
    End Function

End Class

【问题讨论】:

    标签: vb.net visual-studio vb.net-2010 c#-to-vb.net


    【解决方案1】:

    自定义圆形按钮,代码:

    Imports System.Drawing.Drawing2D
    
    
        Class MyButton
            Inherits Button
    
            Private m_TopColor As Color = Color.LightGreen
            Private m_BottomColor As Color = Color.Orange
    
            Public Property TopColor As Color
                Get
                    Return m_TopColor
                End Get
                Set(ByVal value As Color)
                    m_TopColor = value
                    Me.Invalidate()
                End Set
            End Property
    
            Public Property BottomColor As Color
                Get
                    Return m_BottomColor
                End Get
                Set(ByVal value As Color)
                    m_BottomColor = value
                    Me.Invalidate()
                End Set
            End Property
    
            Public Sub New()
                FlatStyle = FlatStyle.Flat
                FlatAppearance.BorderSize = 0
                FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0)
                FlatAppearance.MouseDownBackColor = Color.Transparent
                FlatAppearance.MouseOverBackColor = Color.Transparent
                BackColor = Color.Transparent
                Me.BackgroundImageLayout = ImageLayout.Zoom
    
                AddHandler Me.MouseMove, AddressOf MyMouseMove
                AddHandler Me.MouseLeave, AddressOf MyMouseLeave
    
            End Sub
            Private Sub MyMouseLeave(ByVal sender As Object, ByVal e As EventArgs)
    
            End Sub
    
            Private Sub MyMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    
            End Sub
    
            Private Sub Draw(ByVal rectangle As Rectangle, ByVal g As Graphics, ByVal cusp As Boolean)
    
                Dim span As Integer = 2
                g.SmoothingMode = SmoothingMode.AntiAlias
                Dim myLinearGradientBrush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, m_TopColor, m_BottomColor, 90.0F)
    
    
                g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, 20))
            End Sub
            Private Function DrawRoundRect(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal radius As Integer) As GraphicsPath
                Dim gp As GraphicsPath = New GraphicsPath()
                gp.AddArc(x, y, radius, radius, 180, 90)
                gp.AddArc(width - radius, y, radius, radius, 270, 90)
                gp.AddArc(width - radius, height - radius, radius, radius, 0, 90)
                gp.AddArc(x, height - radius, radius, radius, 90, 90)
                gp.CloseAllFigures()
                Return gp
            End Function
    
            Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
                MyBase.OnPaint(e)
    
                Draw(e.ClipRectangle, e.Graphics, False)
    
            End Sub
    
            Private Function GetVerticalAlignment() As StringAlignment
                Return CType(Math.Log(Me.TextAlign, 2D) / 4, StringAlignment)
            End Function
    
            Private Function GetHorizontalAlignment() As StringAlignment
                Return CType(Math.Log(Me.TextAlign, 2D) Mod 4, StringAlignment)
            End Function
    
        End Class
    

    【讨论】:

    • 我没有看到这个控件有任何变化,我没有在控件中找到圆角和按钮图像的任何属性。有什么,我必须更改代码以使按钮角变圆并显示按钮图标图像?
    • 可以修改下面代码中的30来修改圆角 g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, 30 ))
    • 再次,我正在寻找一个有圆角的按钮控件,可以有顶部和底部颜色,可以有按钮图像和文本。在上面的代码中,角落显示圆形,顶部和底部颜色也显示,但我无法在按钮上看到任何文本和按钮图像,因为它们与您添加的矩形重叠。
    • 当您覆盖 OnPaint 事件时,您需要自己完成所有操作(绘制图像和文本)。操作系统不会为你做这件事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多