MyButton.xaml.vb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Public Class MyButton
  2. '声明
  3. Public Event Click(sender As Object, e As EventArgs) '自定义事件
  4. '自定义属性
  5. Public Uuid As Integer = GetUuid()
  6. Public Property Text As String
  7. Get
  8. Return LabText.Text
  9. End Get
  10. Set(value As String)
  11. LabText.Text = value
  12. End Set
  13. End Property '显示文本
  14. Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(MyButton), New PropertyMetadata(New PropertyChangedCallback(
  15. Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
  16. If Not IsNothing(sender) Then CType(sender, MyButton).LabText.Text = e.NewValue
  17. End Sub)))
  18. Public Property TextPadding As Thickness
  19. Get
  20. Return LabText.Padding
  21. End Get
  22. Set(value As Thickness)
  23. LabText.Padding = value
  24. End Set
  25. End Property
  26. Private _ColorType As ColorState = ColorState.Normal '配色方案
  27. Public Property ColorType As ColorState
  28. Get
  29. Return _ColorType
  30. End Get
  31. Set(value As ColorState)
  32. _ColorType = value
  33. RefreshColor()
  34. End Set
  35. End Property
  36. Public Enum ColorState
  37. Normal = 0
  38. Highlight = 1
  39. Red = 2
  40. End Enum
  41. '属性穿透
  42. Public Shared Shadows ReadOnly PaddingProperty As DependencyProperty = DependencyProperty.Register("Padding", GetType(Thickness), GetType(MyButton), New PropertyMetadata(New PropertyChangedCallback(
  43. Sub(sender As MyButton, e As DependencyPropertyChangedEventArgs) If sender IsNot Nothing Then sender.PanFore.Padding = e.NewValue)))
  44. Public Overloads Property Padding As Thickness
  45. Get
  46. Return PanFore.Padding
  47. End Get
  48. Set(value As Thickness)
  49. PanFore.Padding = value
  50. End Set
  51. End Property
  52. Public Property RealRenderTransform As Transform
  53. Get
  54. Return PanFore.RenderTransform
  55. End Get
  56. Set(value As Transform)
  57. PanFore.RenderTransform = value
  58. End Set
  59. End Property
  60. '自定义事件
  61. Private Const AnimationColorIn As Integer = 100
  62. Private Const AnimationColorOut As Integer = 200
  63. Private Sub RefreshColor(Optional obj = Nothing, Optional e = Nothing) Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded, Me.IsEnabledChanged
  64. Try
  65. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  66. If IsEnabled Then
  67. Select Case ColorType
  68. Case ColorState.Normal
  69. If IsMouseOver Then
  70. '指向(Main 3)
  71. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrush3", AnimationColorIn)}, "MyButton Color " & Uuid)
  72. Else
  73. '普通(Main 1)
  74. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrush1", AnimationColorOut)}, "MyButton Color " & Uuid)
  75. End If
  76. Case ColorState.Highlight
  77. If IsMouseOver Then
  78. '指向(Main 3)
  79. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrush3", AnimationColorIn)}, "MyButton Color " & Uuid)
  80. Else
  81. '高亮(Main 2)
  82. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrush2", AnimationColorOut)}, "MyButton Color " & Uuid)
  83. End If
  84. Case ColorState.Red
  85. If IsMouseOver Then
  86. '红色指向
  87. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrushRedLight", AnimationColorIn)}, "MyButton Color " & Uuid)
  88. Else
  89. '红色
  90. AniStart({AaColor(PanFore, Border.BorderBrushProperty, "ColorBrushRedDark", AnimationColorOut)}, "MyButton Color " & Uuid)
  91. End If
  92. End Select
  93. Else
  94. '不可用(Gray 4)
  95. AniStart({AaColor(PanFore, Border.BorderBrushProperty, ColorGray4 - PanFore.BorderBrush, AnimationColorOut)}, "MyButton Color " & Uuid)
  96. End If
  97. Else
  98. AniStop("MyButton Color " & Uuid)
  99. If IsEnabled Then
  100. Select Case ColorType
  101. Case ColorState.Normal
  102. If IsMouseOver Then
  103. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrush3")
  104. Else
  105. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrush1")
  106. End If
  107. Case ColorState.Highlight
  108. If IsMouseOver Then
  109. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrush3")
  110. Else
  111. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrush2")
  112. End If
  113. Case ColorState.Red
  114. If IsMouseOver Then
  115. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrushRedLight")
  116. Else
  117. PanFore.SetResourceReference(Border.BorderBrushProperty, "ColorBrushRedDark")
  118. End If
  119. End Select
  120. Else
  121. PanFore.BorderBrush = ColorGray4
  122. End If
  123. End If
  124. Catch ex As Exception
  125. Log(ex, "刷新按钮颜色出错")
  126. End Try
  127. End Sub
  128. '实现自定义事件
  129. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  130. If IsMouseDown Then
  131. Log("[Control] 按下按钮:" & Text)
  132. RaiseEvent Click(sender, e)
  133. If Not String.IsNullOrEmpty(Tag) Then
  134. If Tag.ToString.StartsWith("链接-") OrElse Tag.ToString.StartsWith("启动-") Then
  135. Hint("主页自定义按钮语法已更新,且不再兼容老版本语法,请查看新的自定义示例!")
  136. End If
  137. End If
  138. ModEvent.TryStartEvent(EventType, EventData)
  139. End If
  140. End Sub
  141. Public Property EventType As String
  142. Get
  143. Return GetValue(EventTypeProperty)
  144. End Get
  145. Set(ByVal value As String)
  146. SetValue(EventTypeProperty, value)
  147. End Set
  148. End Property
  149. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyButton), New PropertyMetadata(Nothing))
  150. Public Property EventData As String
  151. Get
  152. Return GetValue(EventDataProperty)
  153. End Get
  154. Set(ByVal value As String)
  155. SetValue(EventDataProperty, value)
  156. End Set
  157. End Property
  158. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyButton), New PropertyMetadata(Nothing))
  159. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  160. Private IsMouseDown As Boolean = False
  161. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  162. IsMouseDown = True
  163. Focus()
  164. AniStart({
  165. AaScaleTransform(PanFore, 0.955 - CType(PanFore.RenderTransform, ScaleTransform).ScaleX, 80,, New AniEaseOutFluent(AniEasePower.ExtraStrong)),
  166. AaScaleTransform(PanFore, -0.01, 700,, New AniEaseOutFluent(AniEasePower.Middle))
  167. }, "MyButton Scale " & Uuid)
  168. End Sub
  169. Private Sub Button_MouseEnter() Handles Me.MouseEnter
  170. AniStart(AaColor(PanFore, BackgroundProperty, If(_ColorType = ColorState.Red, "ColorBrushRedBack", "ColorBrush9"), AnimationColorIn), "MyButton Background " & Uuid)
  171. End Sub
  172. Private Sub Button_MouseUp() Handles Me.MouseLeftButtonUp
  173. If Not IsMouseDown Then Exit Sub
  174. IsMouseDown = False
  175. AniStart({
  176. AaScaleTransform(PanFore, 1 - CType(PanFore.RenderTransform, ScaleTransform).ScaleX, 300, 10, New AniEaseOutFluent(AniEasePower.Middle))
  177. }, "MyButton Scale " & Uuid)
  178. End Sub
  179. Private Sub Button_MouseLeave() Handles Me.MouseLeave
  180. AniStart(AaColor(PanFore, BackgroundProperty, "ColorBrushHalfWhite", AnimationColorOut), "MyButton Background " & Uuid)
  181. If Not IsMouseDown Then Exit Sub
  182. IsMouseDown = False
  183. AniStart(AaScaleTransform(PanFore, 1 - CType(PanFore.RenderTransform, ScaleTransform).ScaleX, 800,, New AniEaseOutFluent(AniEasePower.Strong)), "MyButton Scale " & Uuid)
  184. End Sub
  185. End Class