NET 编写五颜六色的自定义控件

 .NET 编写五颜六色的自定义控件

       上一篇只是在窗体上把形状画出来,觉得还是把它写成自定义的button好一点。

写得丑陋不堪,如果大家觉得丑陋依然可以接受那真是太感谢了。具体的编写步骤我就不写了,因为网上已经写很多。 

如果大家有什么建议或意见欢迎大家与我交流。

效果如下图:

 

还是用 VB.NET 代码来写:

 

Imports  System.Drawing
Imports  System.Drawing.Drawing2D
Imports  System.ComponentModel
Public   Class DrawButton
    
Dim _startColor As Color = Color.Red  '开始颜色
    Dim _endColor As Color = Color.White '结束颜色
    Dim _cornerValue As Integer = 5 '矩形圆角的度数
    Dim _centercolor As Color = Color.Black '阴影中间的颜色
    Dim _sColor As Color = SystemColors.ButtonFace ''阴影外边的颜色
    Dim _topHeight As Integer = 15 '最顶层白色层的高度
    Dim _topWidth As Integer = 5 '最顶层缩放大小
    Dim _topstartAphap As Integer = 255 '最顶层开始颜色的透明度
    Dim _topendAphap As Integer = 0 '最顶层结束颜色的透明度
    Dim _topstartcolor As Color = Color.White '最顶层开始颜色
    Dim _topendcolor As Color = Color.White '最顶层结束颜色
    Dim _LinearM As LinearGradientMode = LinearGradientMode.Vertical '最顶层渐变的方向

    
Dim _hoveStartcolor As Color = Color.Red '鼠标在上面开始的颜色
    Dim _hoverendcolor As Color = Color.White '鼠标在上面疆结束的颜色
    Dim _clickstartcolor As Color = Color.Red '单击鼠标时开始的颜色
    Dim _clickendcolor As Color = Color.White '单击鼠标时结束的颜色
    Dim _startOffset As Integer = 8 '阴影与上层图的距离(也叫偏移量)

    
Dim st, ed As Color
    
Dim sOffset As Integer

    
<Description("上层渐变开始颜色")> _
    
<Category("Color")> _
        
Public Property startColor() As Color
        
Get
            
Return _startColor
        
End Get
        
Set(ByVal value As Color)
            _startColor 
= value
        
End Set
    
End Property


    
<Description("上层渐变结束颜色")> _
       
<Category("Color")> _
       
Public Property endColor() As Color
        
Get
            
Return _endColor
        
End Get
        
Set(ByVal value As Color)
            _endColor 
= value
        
End Set
    
End Property

    
<Description("矩形圆角的弯度")> _
    
<Category("Corner")> _
    
Public Property cornerValue() As Integer
        
Get
            
Return _cornerValue
        
End Get
        
Set(ByVal value As Integer)
            _cornerValue 
= value
        
End Set
    
End Property


    
<Description("阴影中心的颜色")> _
    
<Category("backcolor")> _
    
Public Property centerColor() As Color
        
Get
            
Return _centercolor
        
End Get
        
Set(ByVal value As Color)
            _centercolor 
= value
        
End Set
    
End Property


    
<Description("阴影外边颜色")> _
   
<Category("backcolor")> _
   
Public Property sColor() As Color
        
Get
            
Return _sColor
        
End Get
        
Set(ByVal value As Color)
            _sColor 
= value
        
End Set
    
End Property



    
<Description("最上层高度")> _
    
Public Property topHeight() As Integer
        
Get
            
Return _topHeight
        
End Get
        
Set(ByVal value As Integer)
            _topHeight 
= value
        
End Set
    
End Property


    
<Description("最上层缩放宽度")> _
    
Public Property topwidth() As Integer
        
Get
            
Return _topWidth
        
End Get
        
Set(ByVal value As Integer)
            _topWidth 
= value
        
End Set
    
End Property


    
<Description("最上层开始的透明度")> _
     
Public Property topstartAphap() As Integer
        
Get
            
Return _topstartAphap
        
End Get
        
Set(ByVal value As Integer)
            _topstartAphap 
= value
            
If value > 255 Then
                _topstartAphap 
= 255
            
ElseIf value < 0 Then
                _topstartAphap 
= 0
            
End If
        
End Set
    
End Property



    
<Description("最上层结束的透明度")> _
     
Public Property topendAphap() As Integer
        
Get
            
Return _topendAphap
        
End Get
        
Set(ByVal value As Integer)
            _topendAphap 
= value
            
If value > 255 Then
                _topendAphap 
= 255
            
ElseIf value < 0 Then
                _topendAphap 
= 0
            
End If
        
End Set
    
End Property


    
<Description("最上层开始颜色")> _
     
Public Property topstartcolor() As Color
        
Get
            
Return _topstartcolor
        
End Get
        
Set(ByVal value As Color)
            _topstartcolor 
= value
        
End Set
    
End Property


    
<Description("最上层开始颜色")> _
 
Public Property topendcolor() As Color
        
Get
            
Return _topendcolor
        
End Get
        
Set(ByVal value As Color)
            _topendcolor 
= value
        
End Set
    
End Property


    
<Description("最上层渐变的方式")> _
    
Public Property LinearM() As LinearGradientMode
        
Get
            
Return _LinearM
        
End Get
        
Set(ByVal value As LinearGradientMode)
            _LinearM 
= value
        
End Set
    
End Property

    
<Description("开始颜色")> _
    
Public Property startOffset() As Integer
        
Get
            
Return _startOffset
        
End Get
        
Set(ByVal value As Integer)
            _startOffset 
= value
        
End Set
    
End Property

    
<Description("鼠标在上方的开始颜色")> _
    
Public Property hoverstartcolor() As Color
        
Get
            
Return _hoveStartcolor
        
End Get
        
Set(ByVal value As Color)
            _hoveStartcolor 
= value
        
End Set
    
End Property

    
<Description("鼠标在上方的结束颜色")> _
    
Public Property hoverendcolor() As Color
        
Get
            
Return _hoverendcolor
        
End Get
        
Set(ByVal value As Color)
            _hoverendcolor 
= value
        
End Set
    
End Property

    
<Description("鼠标单击开始颜色")> _
    
Public Property clickstartcolor() As Color
        
Get
            
Return _clickstartcolor
        
End Get
        
Set(ByVal value As Color)
            _clickstartcolor 
= value
        
End Set
    
End Property

    
<Description("鼠标单击结束颜色")> _
    
Public Property clickendcolor() As Color
        
Get
            
Return _clickendcolor
        
End Get
        
Set(ByVal value As Color)
            _clickendcolor 
= value
        
End Set
    
End Property


    
'定义鼠标动作枚举
    Public Enum MouseActionType
        hover
        none
        click
    
End Enum

    
Dim mouseAction As MouseActionType = MouseActionType.none

    
'构造方法 
    Sub New()

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

        
' 在 InitializeComponent() 调用之后添加任何初始化。
        st = startColor
        ed 
= endColor
        sOffset 
= startOffset
    
End Sub


    
Protected Overrides Sub OnMouseHover(ByVal e As System.EventArgs)
        
MyBase.OnMouseHover(e)
        mouseAction 
= MouseActionType.hover
    
End Sub

    
Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
        
MyBase.OnMouseClick(e)
        mouseAction 
= MouseActionType.click
    
End Sub


    
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        
MyBase.OnMouseEnter(e)
        mouseAction 
= MouseActionType.hover
    
End Sub

    
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        
MyBase.OnMouseLeave(e)
        mouseAction 
= MouseActionType.none
    
End Sub



    
''' <summary>
    ''' 重写重画方法 
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        
MyBase.OnPaint(e)

        
Dim bit As New Bitmap(Me.Width, Me.Height)
        
Dim g As Graphics = Graphics.FromImage(bit) '实现双缓冲画图
        '缓冲画图其实挺简单的道理,先要要绘制的图形画在自定义的一个内在空间里面
        '其实就是一个bitmap对象或都image对象
        g.Clear(SystemColors.ButtonFace) '清除控件上面的颜色

        g.SmoothingMode 
= SmoothingMode.HighQuality '消除锯齿

        
Select Case mouseAction

            
Case MouseActionType.click
                st 
= clickstartcolor
                ed 
= clickendcolor

            
Case MouseActionType.hover
                sOffset 
= startOffset - 2
                st 
= hoverstartcolor
                ed 
= hoverendcolor
            
Case MouseActionType.none
                st 
= startColor
                ed 
= endColor
                sOffset 
= startOffset
        
End Select

        
'当按钮的可用设为false
        If Me.Enabled = False Then
            st 
= Color.Gray '灰度显示
            ed = Color.White
        
End If

        
' -----------------------------
        Dim rc As New Rectangle(00Me.Width - sOffset, Me.Height - sOffset)
        
Dim path1 As New GraphicsPath
        path1 
= GetgraphicPath(rc, cornerValue)
        
Dim br1 As New LinearGradientBrush(New Point(00), New Point(0Me.Height), st, ed)
        
'----------------------------

        
'创建控件阴影的部分
        Dim rc2 As Rectangle = rc
        rc2.Offset(sOffset, sOffset) 
'偏移图形的位置
        Dim path2 As New GraphicsPath
        path2 
= GetgraphicPath(rc2, cornerValue)
        
Dim br2 As New PathGradientBrush(path2)
        br2.CenterColor 
= centerColor
        br2.SurroundColors 
= New Color() {sColor}
        
'-----------------------------
        '创建控件白色部分
        Dim rc3 As Rectangle = rc
        rc3.Inflate(
-topwidth, -topwidth) '缩放控件水平坐标
        rc3.Height = topHeight '设置矩形高度
        Dim path3 As New GraphicsPath
        path3 
= GetgraphicPath(rc3, cornerValue)
        
Dim br3 As New LinearGradientBrush(rc3, Color.FromArgb(topstartAphap, topstartcolor), Color.FromArgb(topendAphap, topendcolor), LinearM)

        
'绘制文本
        Dim path4 As New GraphicsPath
        
Dim path1bounds As RectangleF = path1.GetBounds
        
Dim rcText As New Rectangle(CInt(path1bounds.X), CInt(path1bounds.Y), CInt(path1bounds.Width), CInt(path1bounds.Height))
        
Dim strformat As New StringFormat
        strformat.Alignment 
= StringAlignment.Center
        strformat.LineAlignment 
= StringAlignment.Center
        path4.AddString(Text, 
Me.Font.FontFamily, CInt(Font.Style), Font.Size, rcText, strformat)
        
Dim txtpen As New Pen(ForeColor, 1)

        
'这几个绘制顺序是要区分的,不然会被覆盖
        g.FillPath(br2, path2)
        g.FillPath(br1, path1)
        g.FillPath(br3, path3)
        g.DrawPath(txtpen, path4)

        
'实现不规则样式(窗体)
        Dim rgn As New Region(path1)
        rgn.Union(path2)
        
Me.Region = rgn

        
Dim gg As Graphics = e.Graphics
        gg.DrawImage(bit, 
00)

    
End Sub



    
'绘制绘图路径对象
    '其实我们想画的一个圆角的矩形
    Public Function GetgraphicPath(ByVal rect As Rectangle, ByVal cornerRadius As IntegerAs GraphicsPath
        
Dim drawpath As New GraphicsPath
        drawpath.AddArc(rect.X, rect.Y, cornerRadius 
* 2, cornerRadius * 218090)
        drawpath.AddLine(rect.X 
+ cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y)
        drawpath.AddArc(rect.X 
+ rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 227090)
        drawpath.AddLine(rect.Right, rect.Y 
+ cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2)
        drawpath.AddArc(rect.X 
+ rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2090)
        drawpath.AddLine(rect.Right 
- cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom)
        drawpath.AddArc(rect.X, rect.Bottom 
- cornerRadius * 2, cornerRadius * 2, cornerRadius * 29090)
        drawpath.AddLine(rect.X, rect.Bottom 
- cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2)
        drawpath.CloseFigure()
        
Return drawpath
    
End Function



End Class


 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值