MyIconButton.xaml.vb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. '触发点击事件
  45. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  46. If IsMouseDown Then
  47. Log("[Control] 按下图标按钮" & If(String.IsNullOrEmpty(Name), "", ":" & Name))
  48. RaiseEvent Click(sender, e)
  49. e.Handled = True
  50. Button_MouseUp()
  51. End If
  52. End Sub
  53. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  54. Private IsMouseDown As Boolean = False
  55. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  56. IsMouseDown = True
  57. Focus()
  58. '指向
  59. AniStart(AaScaleTransform(PanBack, 0.8 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 400,, New AniEaseOutFluent(AniEasePower.Strong)), "MyIconButton Scale " & Uuid)
  60. End Sub
  61. Private Sub Button_MouseUp() Handles Me.MouseLeftButtonUp
  62. If IsMouseDown Then
  63. IsMouseDown = False
  64. AniStart({
  65. AaScaleTransform(PanBack, 1.05 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutBack(AniEasePower.Weak)),
  66. AaScaleTransform(PanBack, -0.05, 250,, New AniEaseOutFluent(AniEasePower.Strong))
  67. }, "MyIconButton Scale " & Uuid)
  68. End If
  69. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  70. End Sub
  71. Private Sub Button_MouseLeave() Handles Me.MouseLeave
  72. IsMouseDown = False
  73. AniStart({
  74. AaScaleTransform(PanBack, 1 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutFluent)
  75. }, "MyIconButton Scale " & Uuid)
  76. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  77. End Sub
  78. '自定义事件
  79. '务必放在 IsMouseDown 更新之后
  80. Private Const AnimationColorIn As Integer = 120
  81. Private Const AnimationColorOut As Integer = 150
  82. Public Sub RefreshAnim() Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded
  83. Try
  84. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  85. If PanBack.Background Is Nothing Then PanBack.Background = New MyColor(0, 255, 255, 255)
  86. If Path.Fill Is Nothing AndAlso Theme = Themes.Black Then Path.Fill = New MyColor(140, 0, 0, 0)
  87. If IsMouseOver Then
  88. '指向
  89. Dim AnimList As New List(Of AniData)
  90. Select Case Theme
  91. Case Themes.Color
  92. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush2", AnimationColorIn))
  93. Case Themes.White
  94. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(50, 255, 255, 255) - PanBack.Background, AnimationColorIn))
  95. Case Themes.Red
  96. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, 76, 76) - Path.Fill, AnimationColorIn))
  97. Case Themes.Black
  98. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(230, 0, 0, 0) - Path.Fill, AnimationColorIn))
  99. Case Themes.Custom
  100. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, Foreground) - Path.Fill, AnimationColorIn))
  101. End Select
  102. AniStart(AnimList, "MyIconButton Color " & Uuid)
  103. Else
  104. '普通
  105. Dim AnimList As New List(Of AniData)
  106. Select Case Theme
  107. Case Themes.Color
  108. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush5", AnimationColorOut))
  109. PanBack.Background = New MyColor(0, 255, 255, 255)
  110. Case Themes.White
  111. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush8", AnimationColorOut))
  112. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(0, 255, 255, 255) - PanBack.Background, AnimationColorOut))
  113. Case Themes.Red
  114. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 255, 76, 76) - Path.Fill, AnimationColorOut))
  115. PanBack.Background = New MyColor(0, 255, 255, 255)
  116. Case Themes.Black
  117. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 0, 0, 0) - Path.Fill, AnimationColorOut))
  118. PanBack.Background = New MyColor(0, 255, 255, 255)
  119. Case Themes.Custom
  120. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, Foreground) - Path.Fill, AnimationColorOut))
  121. PanBack.Background = New MyColor(0, 255, 255, 255)
  122. End Select
  123. AniStart(AnimList, "MyIconButton Color " & Uuid)
  124. End If
  125. Else
  126. AniStop("MyIconButton Color " & Uuid)
  127. Select Case Theme
  128. Case Themes.Color
  129. Path.SetResourceReference(Shape.FillProperty, "ColorBrush5")
  130. Case Themes.White
  131. Path.SetResourceReference(Shape.FillProperty, "ColorBrush8")
  132. Case Themes.Red
  133. Path.Fill = New MyColor(160, 255, 76, 76)
  134. Case Themes.Black
  135. Path.Fill = New MyColor(160, 0, 0, 0)
  136. Case Themes.Custom
  137. Path.Fill = New MyColor(160, Foreground)
  138. End Select
  139. PanBack.Background = New MyColor(0, 255, 255, 255)
  140. End If
  141. Catch ex As Exception
  142. Log(ex, "刷新图标按钮动画状态出错")
  143. End Try
  144. End Sub
  145. End Class
  146. Partial Public Module ModAnimation
  147. Public Sub AniDispose(Control As MyIconButton, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  148. If Not Control.IsHitTestVisible Then Exit Sub
  149. Control.IsHitTestVisible = False
  150. AniStart({
  151. AaScaleTransform(Control, -1.5, 200,, New AniEaseInFluent),
  152. AaCode(Sub()
  153. If RemoveFromChildren Then
  154. CType(Control.Parent, Object).Children.Remove(Control)
  155. Else
  156. Control.Visibility = Visibility.Collapsed
  157. End If
  158. If CallBack IsNot Nothing Then CallBack(Control)
  159. End Sub,, True)
  160. }, "MyIconButton Dispose " & Control.Uuid)
  161. End Sub
  162. End Module