Public Class Form1Friend WithEvents btnTimer As New TimerDim RedValue As IntegerDim GreenValue As IntegerDim BlueValue As IntegerDim RAdd As Integer = 0Dim Gadd As Integer = 0Dim Badd As Integer = 0Dim MyBackColor As ColorPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadbtnTimer.Stop()MyBackColor = Button1.BackColorEnd SubPrivate Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeaveRedValue = CInt(MyBackColor.R) - CInt(Button1.FlatAppearance.MouseOverBackColor.R)GreenValue = CInt(MyBackColor.G) - CInt(Button1.FlatAppearance.MouseOverBackColor.G)BlueValue = CInt(MyBackColor.B) - CInt(Button1.FlatAppearance.MouseOverBackColor.B)RAdd = 0Gadd = 0Badd = 0btnTimer.Interval = 1btnTimer.Start()End SubPrivate Sub btnTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTimer.TickSelect Case RedValueCase Is <>RAdd = RAdd - 1RedValue = RedValue + 1Case Is > 0RAdd = RAdd + 1RedValue = RedValue - 1Case Is = 0RAdd = RAdd + 0End SelectSelect Case GreenValueCase Is <>Gadd = Gadd - 1GreenValue = GreenValue + 1Case Is > 0Gadd = Gadd + 1GreenValue = GreenValue - 1Case Is = 0Gadd = Gadd + 0End SelectSelect Case BlueValueCase Is <>Badd = Badd - 1BlueValue = BlueValue + 1Case Is > 0Badd = Badd + 1BlueValue = BlueValue - 1Case Is = 0Badd = Badd + 0End SelectButton1.BackColor = Color.FromArgb(CInt(Button1.FlatAppearance.MouseOverBackColor.R) + RAdd, CInt(Button1.FlatAppearance.MouseOverBackColor.G) + Gadd, CInt(Button1.FlatAppearance.MouseOverBackColor.B) + Badd)If BlueValue = 0 And RedValue = 0 And GreenValue = 0 ThenbtnTimer.Stop()End If
Thursday, October 8, 2009
Automatically fading buttons in VB.NET
I often find myself obsessing over the finer parts of interface design with my applications. I always thought buttons that innocuously faded from their mouseover color back to their default background color was something really nice and not *too* flashy. Since I couldn't find any material for replicating this in VB.NET I did what all programmers do, I wrote one myself. I hope this code helps anyone attempting to do something similar. Improve upon it if you wish, I am just posting the basic code, it will works best if you encapsulate it in a UserControl...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment