Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.ComponentModel PublicClass DrawButtonClass DrawButton Dim _startColor As Color = Color.Red '开始颜色 Dim _endColor As Color = Color.White '结束颜色 Dim _cornerValue AsInteger=5'矩形圆角的度数 Dim _centercolor As Color = Color.Black '阴影中间的颜色 Dim _sColor As Color = SystemColors.ButtonFace ''阴影外边的颜色 Dim _topHeight AsInteger=15'最顶层白色层的高度 Dim _topWidth AsInteger=5'最顶层缩放大小 Dim _topstartAphap AsInteger=255'最顶层开始颜色的透明度 Dim _topendAphap AsInteger=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 AsInteger=8'阴影与上层图的距离(也叫偏移量) Dim st, ed As Color Dim sOffset AsInteger <Description("上层渐变开始颜色")> _ <Category("Color")> _ PublicProperty startColor()Property startColor() As Color Get Return _startColor EndGet Set(ByVal value As Color) _startColor = value EndSet End Property <Description("上层渐变结束颜色")> _ <Category("Color")> _ PublicProperty endColor()Property endColor() As Color Get Return _endColor EndGet Set(ByVal value As Color) _endColor = value EndSet End Property <Description("矩形圆角的弯度")> _ <Category("Corner")> _ PublicProperty cornerValue()Property cornerValue() AsInteger Get Return _cornerValue EndGet Set(ByVal value AsInteger) _cornerValue = value EndSet End Property <Description("阴影中心的颜色")> _ <Category("backcolor")> _ PublicProperty centerColor()Property centerColor() As Color Get Return _centercolor EndGet Set(ByVal value As Color) _centercolor = value EndSet End Property <Description("阴影外边颜色")> _ <Category("backcolor")> _ PublicProperty sColor()Property sColor() As Color Get Return _sColor EndGet Set(ByVal value As Color) _sColor = value EndSet End Property <Description("最上层高度")> _ PublicProperty topHeight()Property topHeight() AsInteger Get Return _topHeight EndGet Set(ByVal value AsInteger) _topHeight = value EndSet End Property <Description("最上层缩放宽度")> _ PublicProperty topwidth()Property topwidth() AsInteger Get Return _topWidth EndGet Set(ByVal value AsInteger) _topWidth = value EndSet End Property <Description("最上层开始的透明度")> _ PublicProperty topstartAphap()Property topstartAphap() AsInteger Get Return _topstartAphap EndGet Set(ByVal value AsInteger) _topstartAphap = value If value >255Then _topstartAphap =255 ElseIf value <0Then _topstartAphap =0 EndIf EndSet End Property <Description("最上层结束的透明度")> _ PublicProperty topendAphap()Property topendAphap() AsInteger Get Return _topendAphap EndGet Set(ByVal value AsInteger) _topendAphap = value If value >255Then _topendAphap =255 ElseIf value <0Then _topendAphap =0 EndIf EndSet End Property <Description("最上层开始颜色")> _ PublicProperty topstartcolor()Property topstartcolor() As Color Get Return _topstartcolor EndGet Set(ByVal value As Color) _topstartcolor = value EndSet End Property <Description("最上层开始颜色")> _ PublicProperty topendcolor()Property topendcolor() As Color Get Return _topendcolor EndGet Set(ByVal value As Color) _topendcolor = value EndSet End Property <Description("最上层渐变的方式")> _ PublicProperty LinearM()Property LinearM() As LinearGradientMode Get Return _LinearM EndGet Set(ByVal value As LinearGradientMode) _LinearM = value EndSet End Property <Description("开始颜色")> _ PublicProperty startOffset()Property startOffset() AsInteger Get Return _startOffset EndGet Set(ByVal value AsInteger) _startOffset = value EndSet End Property <Description("鼠标在上方的开始颜色")> _ PublicProperty hoverstartcolor()Property hoverstartcolor() As Color Get Return _hoveStartcolor EndGet Set(ByVal value As Color) _hoveStartcolor = value EndSet End Property <Description("鼠标在上方的结束颜色")> _ PublicProperty hoverendcolor()Property hoverendcolor() As Color Get Return _hoverendcolor EndGet Set(ByVal value As Color) _hoverendcolor = value EndSet End Property <Description("鼠标单击开始颜色")> _ PublicProperty clickstartcolor()Property clickstartcolor() As Color Get Return _clickstartcolor EndGet Set(ByVal value As Color) _clickstartcolor = value EndSet End Property <Description("鼠标单击结束颜色")> _ PublicProperty clickendcolor()Property clickendcolor() As Color Get Return _clickendcolor EndGet Set(ByVal value As Color) _clickendcolor = value EndSet End Property '定义鼠标动作枚举 PublicEnum MouseActionTypeEnum MouseActionType hover none click End Enum Dim mouseAction As MouseActionType = MouseActionType.none '构造方法 Sub New()SubNew() ' 此调用是 Windows 窗体设计器所必需的。 InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 st = startColor ed = endColor sOffset = startOffset End Sub ProtectedOverridesSub OnMouseHover()Sub OnMouseHover(ByVal e As System.EventArgs) MyBase.OnMouseHover(e) mouseAction = MouseActionType.hover End Sub ProtectedOverridesSub OnMouseClick()Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseClick(e) mouseAction = MouseActionType.click End Sub ProtectedOverridesSub OnMouseEnter()Sub OnMouseEnter(ByVal e As System.EventArgs) MyBase.OnMouseEnter(e) mouseAction = MouseActionType.hover End Sub ProtectedOverridesSub OnMouseLeave()Sub OnMouseLeave(ByVal e As System.EventArgs) MyBase.OnMouseLeave(e) mouseAction = MouseActionType.none End Sub ''' <summary> ''' 重写重画方法 ''' </summary> ''' <param name="e"></param> ''' <remarks></remarks> ProtectedOverridesSub OnPaint()Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) Dim bit AsNew Bitmap(Me.Width, Me.Height) Dim g As Graphics = Graphics.FromImage(bit) '实现双缓冲画图 '缓冲画图其实挺简单的道理,先要要绘制的图形画在自定义的一个内在空间里面 '其实就是一个bitmap对象或都image对象 g.Clear(SystemColors.ButtonFace) '清除控件上面的颜色 g.SmoothingMode = SmoothingMode.HighQuality '消除锯齿 SelectCase 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 EndSelect '当按钮的可用设为false IfMe.Enabled =FalseThen st = Color.Gray '灰度显示 ed = Color.White EndIf ' ----------------------------- Dim rc AsNew Rectangle(0, 0, Me.Width - sOffset, Me.Height - sOffset) Dim path1 AsNew GraphicsPath path1 = GetgraphicPath(rc, cornerValue) Dim br1 AsNew LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), st, ed) '---------------------------- '创建控件阴影的部分 Dim rc2 As Rectangle = rc rc2.Offset(sOffset, sOffset) '偏移图形的位置 Dim path2 AsNew GraphicsPath path2 = GetgraphicPath(rc2, cornerValue) Dim br2 AsNew PathGradientBrush(path2) br2.CenterColor = centerColor br2.SurroundColors =New Color() {sColor} '----------------------------- '创建控件白色部分 Dim rc3 As Rectangle = rc rc3.Inflate(-topwidth, -topwidth) '缩放控件水平坐标 rc3.Height = topHeight '设置矩形高度 Dim path3 AsNew GraphicsPath path3 = GetgraphicPath(rc3, cornerValue) Dim br3 AsNew LinearGradientBrush(rc3, Color.FromArgb(topstartAphap, topstartcolor), Color.FromArgb(topendAphap, topendcolor), LinearM) '绘制文本 Dim path4 AsNew GraphicsPath Dim path1bounds As RectangleF = path1.GetBounds Dim rcText AsNew Rectangle(CInt(path1bounds.X), CInt(path1bounds.Y), CInt(path1bounds.Width), CInt(path1bounds.Height)) Dim strformat AsNew StringFormat strformat.Alignment = StringAlignment.Center strformat.LineAlignment = StringAlignment.Center path4.AddString(Text, Me.Font.FontFamily, CInt(Font.Style), Font.Size, rcText, strformat) Dim txtpen AsNew Pen(ForeColor, 1) '这几个绘制顺序是要区分的,不然会被覆盖 g.FillPath(br2, path2) g.FillPath(br1, path1) g.FillPath(br3, path3) g.DrawPath(txtpen, path4) '实现不规则样式(窗体) Dim rgn AsNew Region(path1) rgn.Union(path2) Me.Region = rgn Dim gg As Graphics = e.Graphics gg.DrawImage(bit, 0, 0) End Sub '绘制绘图路径对象 '其实我们想画的一个圆角的矩形 PublicFunction GetgraphicPath()Function GetgraphicPath(ByVal rect As Rectangle, ByVal cornerRadius AsInteger) As GraphicsPath Dim drawpath AsNew GraphicsPath drawpath.AddArc(rect.X, rect.Y, cornerRadius *2, cornerRadius *2, 180, 90) 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 *2, 270, 90) 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 *2, 0, 90) 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 *2, 90, 90) drawpath.AddLine(rect.X, rect.Bottom - cornerRadius *2, rect.X, rect.Y + cornerRadius *2) drawpath.CloseFigure() Return drawpath End Function End Class