MyButton.xaml.vb 8.1 KB

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