Как анимировать нажатие кнопки? - Visual Basic .NET
Формулировка задачи:
Всем здравствуйте.
Есть изображение, соответствующее нажатому состоянию и изображение, соответствующее отпущенному состоянию кнопки.
Когда-то в VB-6 я вставлял в коде в события MouseUp и MouseDown элемента управления Image команды загрузки соответствующих изображений. Получалась довольно наглядная анимация процесса нажатия кнопки. В VB.Net на эту тему есть что-либо более продвинутое?
Решение задачи: «Как анимировать нажатие кнопки?»
textual
Листинг программы
Public Class Form1
#Region " Скин кнопки 1"
Private Sub Button1_MouseLeave(sender As Object, e As System.EventArgs) Handles Button1.MouseLeave, MyBase.Load
Button1.BackgroundImage = GetButtonSkin(Image.FromFile("1.png"), 1, True)
End Sub
Private Sub Button1_MouseEnter(sender As Object, e As System.EventArgs) Handles Button1.MouseEnter, Button1.MouseUp
sender.backgroundimage = GetButtonSkin(Image.FromFile("1.png"), 1)
End Sub
Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
sender.backgroundimage = GetButtonSkin(Image.FromFile("1.png"), 3)
End Sub
#End Region
Public Function GetButtonSkin(SourceImage As Image, Optional Return_Skin As Integer = Nothing, Optional ToGray As Boolean = False) As Image
Dim NewButtonImage As New Bitmap(CInt(SourceImage.Width / 3), SourceImage.Height)
Dim myGraphics As Graphics = Graphics.FromImage(NewButtonImage)
Dim ButtonSize As New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
Dim getState As Rectangle
If Return_Skin = 1 Then ' Normal State
getState = New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
ElseIf Return_Skin = 2 Then ' Hover State
getState = New Rectangle(SourceImage.Width / 3, 0, SourceImage.Width / 3, SourceImage.Height)
ElseIf Return_Skin = 3 Then ' Dowm State
getState = New Rectangle(SourceImage.Width / 3 * 2, 0, SourceImage.Width / 3, SourceImage.Height)
Else ' If not Indicated - return Normal State
getState = New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
End If
myGraphics.DrawImage(SourceImage, ButtonSize, getState, GraphicsUnit.Pixel)
Dim PixelColor As Color : Dim PaintToGray As Byte
If ToGray Then
For x As Integer = 0 To NewButtonImage.Width - 1
For y As Integer = 0 To NewButtonImage.Height - 1
PixelColor = NewButtonImage.GetPixel(x, y)
'Если пиксель непрозрачный, то перекрашиваем его в серый цвет
If PixelColor <> Color.FromArgb(0) Then
PaintToGray = 0.3 * PixelColor.R + 0.59 * PixelColor.G + 0.11 * PixelColor.B
PixelColor = Color.FromArgb(PaintToGray, PaintToGray, PaintToGray)
NewButtonImage.SetPixel(x, y, PixelColor)
End If
Next
Next
End If
Return NewButtonImage
End Function
End Class