纯代码绘制漂亮特效动画按钮

dylike 2011-12-29 03:55:24
加精


Imports System.ComponentModel
<DefaultEvent("Click")> _
Public Class DSButton
Private _ButtonColor As Color = Color.White
Private SF As New System.Drawing.StringFormat
Private _Text As String
<DefaultValue(True)>
Public Property ButtonColor As Color
Get
Return _ButtonColor
End Get
Set(ByVal value As Color)
_ButtonColor = value
MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50))
End Set
End Property
Public Property RoundRectValue As Integer = 10
Private nIndex As Integer = 0
Private IsMouseEnter As Boolean = False
Public Property IsShowAnimate As Boolean = False
Public Property ButtonText As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50))
End Set
End Property
Private _TextColor As Color = Color.White
Public Property TextColor As Color
Get
Return _TextColor
End Get
Set(ByVal value As Color)
_TextColor = value
MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50))
End Set
End Property

Private Sub DSButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)
SetStyle(ControlStyles.Selectable, True)
SF.LineAlignment = StringAlignment.Center
SF.Alignment = StringAlignment.Center
MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50))
End Sub
Private Sub MakeRoundedRect(ByVal Rounded As Integer, ByVal Ct As Control, ByVal ButtonColor As Color)
If Ct.BackgroundImage IsNot Nothing Then Ct.BackgroundImage.Dispose()
Ct.BackgroundImage = New Bitmap(Ct.Width, Ct.Height)
Dim WW, HH As Integer
WW = Ct.Width - 1
HH = Ct.Height - 1
Using G As Graphics = Graphics.FromImage(Ct.BackgroundImage)
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
Using Gp As New Drawing2D.GraphicsPath
Gp.AddArc(New Rectangle(0, 0, Rounded, Rounded), 180, 90)
Gp.AddArc(New Rectangle(WW - Rounded, 0, Rounded, Rounded), -90, 90)
Gp.AddArc(New Rectangle(WW - Rounded, HH - Rounded, Rounded, Rounded), 0, 90)
Gp.AddArc(New Rectangle(0, HH - Rounded, Rounded, Rounded), 90, 90)
Gp.AddLine(New Point(0, HH - Rounded), New Point(0, Rounded / 2))
Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH), ControlPaint.Dark(ButtonColor, 0.5), ButtonColor)
G.FillPath(Lg, Gp)
G.DrawPath(Pens.Black, Gp)
End Using
End Using
WW = WW - 3
HH = HH - 3
Using Gp As New Drawing2D.GraphicsPath
Gp.AddArc(New Rectangle(3, 3, Rounded, Rounded), 180, 90)
Gp.AddArc(New Rectangle(WW - Rounded, 3, Rounded, Rounded), -90, 90)
Gp.AddArc(New Rectangle(WW - Rounded, HH / 2 - Rounded - 1, Rounded, Rounded), 0, 90)
Gp.AddArc(New Rectangle(3, HH / 2 - Rounded - 1, Rounded, Rounded), 90, 90)
Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH / 2), Color.FromArgb(220, 255, 255, 255), Color.FromArgb(50, 255, 255, 255))
G.FillPath(Lg, Gp)
End Using
End Using
Using Gp As New Drawing2D.GraphicsPath
Gp.AddEllipse(New Rectangle(3, HH / 2 + 10, WW, HH / 2))
Using Lg As New Drawing2D.PathGradientBrush(Gp)
Lg.CenterColor = Color.FromArgb(150, 255, 255, 255)
Lg.SurroundColors = New Color() {Color.Transparent}
Gp.FillMode = Drawing2D.FillMode.Winding
G.FillPath(Lg, Gp)
End Using
End Using
Try
If _Text.Length <> 0 Then G.DrawString(_Text, Me.Font, New SolidBrush(TextColor), New Rectangle(0, 0, Me.Width, Me.Height), SF)
Catch
End Try
End Using
End Sub

Private Sub DSButton_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
End Sub

Private Sub DSButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
MakeRoundedRect(RoundRectValue, Me, Color.Black)
End If
End Sub

Private Sub DSButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
If DesignMode = False Then
IsMouseEnter = True
Timer1.Enabled = True
End If
End Sub

Private Sub DSButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
If DesignMode = False Then
IsMouseEnter = False
Timer1.Enabled = True
End If
End Sub

Private Sub DSButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
MakeRoundedRect(RoundRectValue, Me, _ButtonColor)
End Sub

Private Sub DSButton_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
If Me.IsHandleCreated Then
MakeRoundedRect(RoundRectValue, Me, ButtonColor)
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Select Case IsMouseEnter
Case True
If IsShowAnimate = True Then
nIndex = IIf(nIndex + 30 >= 225, 255, nIndex + 30)
If nIndex >= 255 Then Timer1.Enabled = False
Else
nIndex = 255
Timer1.Enabled = False
End If
Case False
nIndex = IIf(nIndex - 20 <= 50, 50, nIndex - 20)
If nIndex <= 50 Then Timer1.Enabled = False
End Select
Try
MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * nIndex, ButtonColor.G / 255 * nIndex, ButtonColor.B / 255 * nIndex))
Catch
End Try
End Sub
End Class

源码下载
...全文
6752 104 打赏 收藏 转发到动态 举报
写回复
用AI写文章
104 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼主真是强悍啊
nxlzb1 2014-08-15
  • 打赏
  • 举报
回复
按钮居然可以这么做 我还是第一次看见哦 厉害
wzuomin 2013-07-02
  • 打赏
  • 举报
回复
效果很棒,楼主出的东西就是那么给力哦!
wan123qwe 2013-06-09
  • 打赏
  • 举报
回复
很不错,学习一哈
s34855 2013-05-28
  • 打赏
  • 举报
回复
不错,但是能不能写点注释吖。
BG 2012-02-20
  • 打赏
  • 举报
回复
通过纯代码绘制实现有什么好处呢?
是不是WinForm下的界面用GDI的方法来绘制算是正确的实现方式呢?
tankremark1212 2012-01-06
  • 打赏
  • 举报
回复
很漂亮。赞一个!
bob008 2012-01-06
  • 打赏
  • 举报
回复
这不吃饱了撑的吗,直接找个图片画上去不就完了
yemx21 2012-01-03
  • 打赏
  • 举报
回复
NB,不错
足球中国 2012-01-03
  • 打赏
  • 举报
回复
[Quote=引用 88 楼 dylike 的回复:]

引用 84 楼 sp1234 的回复:

不管你怎样实现显示,我用“纯代码”调用一下你的代码,这就算是“纯代码绘制”了!

我才不管你如何在内部实现,才不管你有没有资源文件。


我不管是谁给"纯代码"定义的,但你不得不说上面只有代码,什么?那些不是代码?
[/Quote]
对于有些水平不咋地乱批评人的。不要理会。
  • 打赏
  • 举报
回复
不错,2D用的挺不错,VB真的很少有人这么玩了
psdty 2012-01-03
  • 打赏
  • 举报
回复
楼主强人!
吟风笛 2012-01-03
  • 打赏
  • 举报
回复
真的很不错啊
txj2002 2012-01-02
  • 打赏
  • 举报
回复
的确做得不错,希望有更多的!
仨枝烟 2012-01-02
  • 打赏
  • 举报
回复
这个不错啊 做的很美 视觉的享受啊
dylike 2012-01-02
  • 打赏
  • 举报
回复
[Quote=引用 84 楼 sp1234 的回复:]

不管你怎样实现显示,我用“纯代码”调用一下你的代码,这就算是“纯代码绘制”了!

我才不管你如何在内部实现,才不管你有没有资源文件。
[/Quote]

我不管是谁给"纯代码"定义的,但你不得不说上面只有代码,什么?那些不是代码?
dylike 2012-01-02
  • 打赏
  • 举报
回复
[Quote=引用 84 楼 sp1234 的回复:]

不管你怎样实现显示,我用“纯代码”调用一下你的代码,这就算是“纯代码绘制”了!

我才不管你如何在内部实现,才不管你有没有资源文件。
[/Quote]


我们属于低等人,哪能和你们每天数钱的高等人比,我们能做出这些已经不容易了,当然对你们高等人来说太容易太简单.
fji57 2012-01-02
  • 打赏
  • 举报
回复
学习了,看来vb.net的功能学是很强大的。
  • 打赏
  • 举报
回复
从学习的角度,这多了一种demo。

从使用的角度,其实假设你每一次都用代码绘制一个东西,与把以前的绘制结果保存下来然后复用,哪一个都是可以的。
  • 打赏
  • 举报
回复
不管你怎样实现显示,我用“纯代码”调用一下你的代码,这就算是“纯代码绘制”了!

我才不管你如何在内部实现,才不管你有没有资源文件。
加载更多回复(54)

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧