Как анимировать нажатие кнопки? - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Всем здравствуйте. Есть изображение, соответствующее нажатому состоянию и изображение, соответствующее отпущенному состоянию кнопки. Когда-то в VB-6 я вставлял в коде в события MouseUp и MouseDown элемента управления Image команды загрузки соответствующих изображений. Получалась довольно наглядная анимация процесса нажатия кнопки. В VB.Net на эту тему есть что-либо более продвинутое?

Решение задачи: «Как анимировать нажатие кнопки?»

textual
Листинг программы
  1. Public Class Form1
  2.  
  3. #Region "    Скин кнопки 1"
  4.     Private Sub Button1_MouseLeave(sender As Object, e As System.EventArgs) Handles Button1.MouseLeave, MyBase.Load
  5.         Button1.BackgroundImage = GetButtonSkin(Image.FromFile("1.png"), 1, True)
  6.     End Sub
  7.     Private Sub Button1_MouseEnter(sender As Object, e As System.EventArgs) Handles Button1.MouseEnter, Button1.MouseUp
  8.         sender.backgroundimage = GetButtonSkin(Image.FromFile("1.png"), 1)
  9.     End Sub
  10.     Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
  11.         sender.backgroundimage = GetButtonSkin(Image.FromFile("1.png"), 3)
  12.     End Sub
  13. #End Region
  14.  
  15.     Public Function GetButtonSkin(SourceImage As Image, Optional Return_Skin As Integer = Nothing, Optional ToGray As Boolean = False) As Image
  16.         Dim NewButtonImage As New Bitmap(CInt(SourceImage.Width / 3), SourceImage.Height)
  17.         Dim myGraphics As Graphics = Graphics.FromImage(NewButtonImage)
  18.         Dim ButtonSize As New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
  19.         Dim getState As Rectangle
  20.  
  21.         If Return_Skin = 1 Then ' Normal State
  22.             getState = New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
  23.         ElseIf Return_Skin = 2 Then ' Hover State
  24.             getState = New Rectangle(SourceImage.Width / 3, 0, SourceImage.Width / 3, SourceImage.Height)
  25.         ElseIf Return_Skin = 3 Then ' Dowm State
  26.             getState = New Rectangle(SourceImage.Width / 3 * 2, 0, SourceImage.Width / 3, SourceImage.Height)
  27.         Else ' If not Indicated - return Normal State
  28.             getState = New Rectangle(0, 0, SourceImage.Width / 3, SourceImage.Height)
  29.         End If
  30.  
  31.         myGraphics.DrawImage(SourceImage, ButtonSize, getState, GraphicsUnit.Pixel)
  32.  
  33.         Dim PixelColor As Color : Dim PaintToGray As Byte
  34.         If ToGray Then
  35.             For x As Integer = 0 To NewButtonImage.Width - 1
  36.                 For y As Integer = 0 To NewButtonImage.Height - 1
  37.                     PixelColor = NewButtonImage.GetPixel(x, y)
  38.                     'Если пиксель непрозрачный, то перекрашиваем его в серый цвет
  39.                     If PixelColor <> Color.FromArgb(0) Then
  40.                         PaintToGray = 0.3 * PixelColor.R + 0.59 * PixelColor.G + 0.11 * PixelColor.B
  41.                         PixelColor = Color.FromArgb(PaintToGray, PaintToGray, PaintToGray)
  42.                         NewButtonImage.SetPixel(x, y, PixelColor)
  43.                     End If
  44.                 Next
  45.             Next
  46.         End If
  47.  
  48.         Return NewButtonImage
  49.     End Function
  50. End Class

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.083 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут