MyIconButton.xaml.vb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Red
  29. Custom
  30. End Enum
  31. Public Property Theme As Themes = Themes.Color
  32. Private _Foreground As New SolidColorBrush(Color.FromRgb(128, 128, 128))
  33. Public Property Foreground As SolidColorBrush
  34. Get
  35. Return _Foreground
  36. End Get
  37. Set(value As SolidColorBrush)
  38. _Foreground = value
  39. AniControlEnabled += 1
  40. RefreshAnim()
  41. AniControlEnabled -= 1
  42. End Set
  43. End Property
  44. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  45. Private IsMouseDown As Boolean = False
  46. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  47. If Not IsMouseDown Then Return
  48. Log("[Control] 按下图标按钮" & If(String.IsNullOrEmpty(Name), "", ":" & Name))
  49. RaiseEvent Click(sender, e)
  50. e.Handled = True
  51. Button_MouseUp()
  52. RaiseCustomEvent() '自定义事件
  53. End Sub
  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. '务必放在 IsMouseDown 更新之后
  78. Private Const AnimationColorIn As Integer = 120
  79. Private Const AnimationColorOut As Integer = 150
  80. Public Sub RefreshAnim() Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded
  81. Try
  82. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  83. If PanBack.Background Is Nothing Then PanBack.Background = New MyColor(0, 255, 255, 255)
  84. If Path.Fill Is Nothing Then
  85. Select Case Theme
  86. Case Themes.Red
  87. Path.Fill = New MyColor(160, 255, 76, 76)
  88. Case Themes.Black
  89. Path.Fill = New MyColor(160, 0, 0, 0)
  90. Case Themes.Custom
  91. Path.Fill = New MyColor(160, Foreground)
  92. End Select
  93. End If
  94. If IsMouseOver Then
  95. '指向
  96. Dim AnimList As New List(Of AniData)
  97. Select Case Theme
  98. Case Themes.Color
  99. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush2", AnimationColorIn))
  100. Case Themes.White
  101. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(50, 255, 255, 255) - PanBack.Background, AnimationColorIn))
  102. Case Themes.Red
  103. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, 76, 76) - Path.Fill, AnimationColorIn))
  104. Case Themes.Black
  105. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(230, 0, 0, 0) - Path.Fill, AnimationColorIn))
  106. Case Themes.Custom
  107. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, Foreground) - Path.Fill, AnimationColorIn))
  108. End Select
  109. AniStart(AnimList, "MyIconButton Color " & Uuid)
  110. Else
  111. '普通
  112. Dim AnimList As New List(Of AniData)
  113. Select Case Theme
  114. Case Themes.Color
  115. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush4", AnimationColorOut))
  116. PanBack.Background = New MyColor(0, 255, 255, 255)
  117. Case Themes.White
  118. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush8", AnimationColorOut))
  119. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(0, 255, 255, 255) - PanBack.Background, AnimationColorOut))
  120. Case Themes.Red
  121. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 255, 76, 76) - Path.Fill, AnimationColorOut))
  122. PanBack.Background = New MyColor(0, 255, 255, 255)
  123. Case Themes.Black
  124. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 0, 0, 0) - Path.Fill, AnimationColorOut))
  125. PanBack.Background = New MyColor(0, 255, 255, 255)
  126. Case Themes.Custom
  127. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, Foreground) - Path.Fill, AnimationColorOut))
  128. PanBack.Background = New MyColor(0, 255, 255, 255)
  129. End Select
  130. AniStart(AnimList, "MyIconButton Color " & Uuid)
  131. End If
  132. Else
  133. AniStop("MyIconButton Color " & Uuid)
  134. Select Case Theme
  135. Case Themes.Color
  136. Path.SetResourceReference(Shape.FillProperty, "ColorBrush5")
  137. Case Themes.White
  138. Path.SetResourceReference(Shape.FillProperty, "ColorBrush8")
  139. Case Themes.Red
  140. Path.Fill = New MyColor(160, 255, 76, 76)
  141. Case Themes.Black
  142. Path.Fill = New MyColor(160, 0, 0, 0)
  143. Case Themes.Custom
  144. Path.Fill = New MyColor(160, Foreground)
  145. End Select
  146. PanBack.Background = New MyColor(0, 255, 255, 255)
  147. End If
  148. Catch ex As Exception
  149. Log(ex, "刷新图标按钮动画状态出错")
  150. End Try
  151. End Sub
  152. End Class
  153. Partial Public Module ModAnimation
  154. Public Sub AniDispose(Control As MyIconButton, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  155. If Not Control.IsHitTestVisible Then Return
  156. Control.IsHitTestVisible = False
  157. AniStart({
  158. AaScaleTransform(Control, -1.5, 200,, New AniEaseInFluent),
  159. AaCode(Sub()
  160. If RemoveFromChildren Then
  161. CType(Control.Parent, Object).Children.Remove(Control)
  162. Else
  163. Control.Visibility = Visibility.Collapsed
  164. End If
  165. If CallBack IsNot Nothing Then CallBack(Control)
  166. End Sub,, True)
  167. }, "MyIconButton Dispose " & Control.Uuid)
  168. End Sub
  169. End Module