MyTextButton.vb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Public Class MyTextButton
  2. Inherits Label
  3. Public Event Click(sender As Object, e As EventArgs)
  4. '基础
  5. Public Uuid As Integer = GetUuid()
  6. Public Sub New()
  7. SetResourceReference(ForegroundProperty, "ColorBrush1")
  8. Background = ColorSemiTransparent
  9. End Sub
  10. '文本
  11. Public Property Text As String
  12. Get
  13. Return GetValue(TextProperty)
  14. End Get
  15. Set(value As String)
  16. SetValue(TextProperty, value)
  17. End Set
  18. End Property
  19. Public Shared ReadOnly TextProperty As DependencyProperty =
  20. DependencyProperty.Register("Text", GetType(String), GetType(MyTextButton), New PropertyMetadata("",
  21. Sub(sender As MyTextButton, e As DependencyPropertyChangedEventArgs)
  22. If e.OldValue = e.NewValue Then Return
  23. AniStart({
  24. AaOpacity(sender, -sender.Opacity, 50),
  25. AaCode(Sub() sender.Content = e.NewValue,, True),
  26. AaOpacity(sender, 1, 170)
  27. }, "MyTextButton Text " & sender.Uuid)
  28. End Sub))
  29. '鼠标事件
  30. Public IsMouseDown As Boolean = False
  31. Private Sub MyTextButton_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonDown
  32. IsMouseDown = True
  33. e.Handled = True
  34. End Sub
  35. Private Sub MyTextButton_MouseLeave() Handles Me.MouseLeave
  36. IsMouseDown = False
  37. End Sub
  38. Private Sub MyTextButton_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonUp
  39. If Not IsMouseDown Then Return
  40. IsMouseDown = False
  41. Logger.Info($"按下文本按钮:{Text}")
  42. RaiseEvent Click(Me, Nothing)
  43. RaiseCustomEvent()
  44. e.Handled = True
  45. End Sub
  46. '指向动画
  47. Private Const AnimationTimeIn As Integer = 100
  48. Private Const AnimationTimeOut As Integer = 200
  49. Private ColorName As String
  50. Private Sub RefreshColor() Handles Me.MouseEnter, Me.MouseLeave, Me.IsEnabledChanged, Me.MouseLeftButtonDown, Me.MouseLeftButtonUp
  51. '判断当前颜色
  52. Dim ForeName As String
  53. Dim Time As Integer
  54. If IsMouseDown Then
  55. ForeName = "ColorBrush4"
  56. Time = 30
  57. ElseIf IsMouseOver Then
  58. ForeName = "ColorBrush3"
  59. Time = AnimationTimeIn
  60. Else
  61. ForeName = "ColorBrush1"
  62. Time = AnimationTimeOut
  63. End If
  64. '重复性验证
  65. If ColorName = ForeName Then Return
  66. ColorName = ForeName
  67. '触发颜色动画
  68. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  69. '有动画
  70. AniStart(AaColor(Me, ForegroundProperty, ForeName, Time), "MyTextButton Color " & Uuid)
  71. Else
  72. '无动画
  73. AniStop("MyTextButton Color " & Uuid)
  74. SetResourceReference(ForegroundProperty, ForeName)
  75. End If
  76. End Sub
  77. End Class