MyIconButton.xaml.vb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Public Class MyIconButton
  2. '声明
  3. Public Event Click(sender As Object, e As EventArgs) '自定义事件
  4. '自定义属性
  5. Public Uuid As Integer = GetUuid()
  6. Public Property Logo As String
  7. Get
  8. Return Path.Data.ToString
  9. End Get
  10. Set(value As String)
  11. Path.Data = (New GeometryConverter).ConvertFromString(value)
  12. End Set
  13. End Property
  14. Private _LogoScale As Double = 1
  15. Public Property LogoScale() As Double
  16. Get
  17. Return _LogoScale
  18. End Get
  19. Set(value As Double)
  20. _LogoScale = value
  21. If Not IsNothing(Path) Then Path.RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale}
  22. End Set
  23. End Property
  24. Public Enum Themes
  25. Color
  26. White
  27. Black
  28. Custom
  29. End Enum
  30. Public Property Theme As Themes = Themes.Color
  31. Private _Foreground As New SolidColorBrush(Color.FromRgb(128, 128, 128))
  32. Public Property Foreground As SolidColorBrush
  33. Get
  34. Return _Foreground
  35. End Get
  36. Set(value As SolidColorBrush)
  37. _Foreground = value
  38. AniControlEnabled += 1
  39. RefreshAnim()
  40. AniControlEnabled -= 1
  41. End Set
  42. End Property
  43. '触发点击事件
  44. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  45. If IsMouseDown Then
  46. Log("[Control] 按下图标按钮" & If(String.IsNullOrEmpty(Name), "", ":" & Name))
  47. RaiseEvent Click(sender, e)
  48. e.Handled = True
  49. Button_MouseUp()
  50. End If
  51. End Sub
  52. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  53. Private IsMouseDown As Boolean = False
  54. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  55. IsMouseDown = True
  56. Focus()
  57. '指向
  58. AniStart(AaScaleTransform(PanBack, 0.8 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 400,, New AniEaseOutFluent(AniEasePower.Strong)), "MyIconButton Scale " & Uuid)
  59. End Sub
  60. Private Sub Button_MouseUp() Handles Me.MouseLeftButtonUp
  61. If IsMouseDown Then
  62. IsMouseDown = False
  63. AniStart({
  64. AaScaleTransform(PanBack, 1.05 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutBack(AniEasePower.Weak)),
  65. AaScaleTransform(PanBack, -0.05, 250,, New AniEaseOutFluent(AniEasePower.Strong))
  66. }, "MyIconButton Scale " & Uuid)
  67. End If
  68. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  69. End Sub
  70. Private Sub Button_MouseLeave() Handles Me.MouseLeave
  71. IsMouseDown = False
  72. AniStart({
  73. AaScaleTransform(PanBack, 1 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutFluent)
  74. }, "MyIconButton Scale " & Uuid)
  75. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  76. End Sub
  77. '自定义事件
  78. '务必放在 IsMouseDown 更新之后
  79. Private Const AnimationColorIn As Integer = 120
  80. Private Const AnimationColorOut As Integer = 150
  81. Public Sub RefreshAnim() Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded
  82. Try
  83. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  84. If PanBack.Background Is Nothing Then PanBack.Background = New MyColor(0, 255, 255, 255)
  85. If Path.Fill Is Nothing AndAlso Theme = Themes.Black Then Path.Fill = New MyColor(140, 0, 0, 0)
  86. If IsMouseOver Then
  87. '指向
  88. Dim AnimList As New List(Of AniData)
  89. Select Case Theme
  90. Case Themes.Color
  91. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush2", AnimationColorIn))
  92. Case Themes.White
  93. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(50, 255, 255, 255) - PanBack.Background, AnimationColorIn))
  94. Case Themes.Black
  95. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(230, 0, 0, 0) - Path.Fill, AnimationColorIn))
  96. Case Themes.Custom
  97. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, Foreground) - Path.Fill, AnimationColorIn))
  98. End Select
  99. AniStart(AnimList, "MyIconButton Color " & Uuid)
  100. Else
  101. '普通
  102. Dim AnimList As New List(Of AniData)
  103. Select Case Theme
  104. Case Themes.Color
  105. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush4", AnimationColorOut))
  106. PanBack.Background = New MyColor(0, 255, 255, 255)
  107. Case Themes.White
  108. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush8", AnimationColorOut))
  109. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(0, 255, 255, 255) - PanBack.Background, AnimationColorOut))
  110. Case Themes.Black
  111. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 0, 0, 0) - Path.Fill, AnimationColorOut))
  112. PanBack.Background = New MyColor(0, 255, 255, 255)
  113. Case Themes.Custom
  114. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, Foreground) - Path.Fill, AnimationColorOut))
  115. PanBack.Background = New MyColor(0, 255, 255, 255)
  116. End Select
  117. AniStart(AnimList, "MyIconButton Color " & Uuid)
  118. End If
  119. Else
  120. AniStop("MyIconButton Color " & Uuid)
  121. Select Case Theme
  122. Case Themes.Color
  123. Path.SetResourceReference(Shape.FillProperty, "ColorBrush5")
  124. Case Themes.White
  125. Path.SetResourceReference(Shape.FillProperty, "ColorBrush8")
  126. Case Themes.Black
  127. Path.Fill = New MyColor(160, 0, 0, 0)
  128. Case Themes.Custom
  129. Path.Fill = New MyColor(160, Foreground)
  130. End Select
  131. PanBack.Background = New MyColor(0, 255, 255, 255)
  132. End If
  133. Catch ex As Exception
  134. Log(ex, "刷新图标按钮动画状态出错")
  135. End Try
  136. End Sub
  137. End Class
  138. Partial Public Module ModAnimation
  139. Public Sub AniDispose(Control As MyIconButton, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  140. If Not Control.IsHitTestVisible Then Exit Sub
  141. Control.IsHitTestVisible = False
  142. AniStart({
  143. AaScaleTransform(Control, -1.5, 200,, New AniEaseInFluent),
  144. AaCode(Sub()
  145. If RemoveFromChildren Then
  146. CType(Control.Parent, Object).Children.Remove(Control)
  147. Else
  148. Control.Visibility = Visibility.Collapsed
  149. End If
  150. If CallBack IsNot Nothing Then CallBack(Control)
  151. End Sub,, True)
  152. }, "MyIconButton Dispose " & Control.Uuid)
  153. End Sub
  154. End Module