MyIconTextButton.xaml.vb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Imports System.Windows.Markup
  2. <ContentProperty("Inlines")>
  3. Public Class MyIconTextButton
  4. '基础
  5. Public Uuid As Integer = GetUuid()
  6. Public Event Check(sender As Object, raiseByMouse As Boolean)
  7. Public Event Change(sender As Object, raiseByMouse As Boolean)
  8. '自定义属性
  9. Public Property Logo As String
  10. Get
  11. Return ShapeLogo.Data.ToString
  12. End Get
  13. Set(value As String)
  14. ShapeLogo.Data = (New GeometryConverter).ConvertFromString(value)
  15. End Set
  16. End Property
  17. Private _LogoScale As Double = 1
  18. Public Property LogoScale() As Double
  19. Get
  20. Return _LogoScale
  21. End Get
  22. Set(value As Double)
  23. _LogoScale = value
  24. If Not IsNothing(ShapeLogo) Then ShapeLogo.RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale}
  25. End Set
  26. End Property
  27. Public ReadOnly Property Inlines As InlineCollection
  28. Get
  29. Return LabText.Inlines
  30. End Get
  31. End Property
  32. Public Property Text As String
  33. Get
  34. Return GetValue(TextProperty)
  35. End Get
  36. Set(value As String)
  37. SetValue(TextProperty, value)
  38. End Set
  39. End Property '内容
  40. Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(MyIconTextButton), New PropertyMetadata(New PropertyChangedCallback(
  41. Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
  42. If Not IsNothing(sender) Then CType(sender, MyIconTextButton).LabText.Text = e.NewValue
  43. End Sub)))
  44. Public Enum ColorState
  45. Black
  46. Highlight
  47. End Enum
  48. Public Property ColorType As ColorState
  49. Get
  50. Return GetValue(ColorTypeProperty)
  51. End Get
  52. Set(value As ColorState)
  53. If ColorType = value Then Return
  54. SetValue(ColorTypeProperty, value)
  55. RefreshColor()
  56. End Set
  57. End Property '颜色类别
  58. Public Shared ReadOnly ColorTypeProperty As DependencyProperty =
  59. DependencyProperty.Register("ColorType", GetType(ColorState), GetType(MyIconTextButton), New PropertyMetadata(ColorState.Black))
  60. '点击事件
  61. Public Event Click(sender As Object, e As RouteEventArgs)
  62. Private IsMouseDown As Boolean = False
  63. Private Sub MyIconTextButton_MouseUp() Handles Me.MouseLeftButtonUp
  64. If Not IsMouseDown Then Return
  65. Log("[Control] 按下带图标按钮:" & Text)
  66. IsMouseDown = False
  67. RaiseEvent Click(Me, New RouteEventArgs(True))
  68. RaiseCustomEvent() '自定义事件
  69. RefreshColor()
  70. End Sub
  71. Private Sub MyIconTextButton_MouseDown() Handles Me.MouseLeftButtonDown
  72. IsMouseDown = True
  73. RefreshColor()
  74. End Sub
  75. Private Sub MyIconTextButton_MouseLeave() Handles Me.MouseLeave
  76. IsMouseDown = False
  77. RefreshColor()
  78. End Sub
  79. '动画
  80. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  81. Private Const AnimationTimeOfMouseOut As Integer = 150 '鼠标移出动画长度
  82. Private Sub RefreshColor(Optional obj = Nothing, Optional e = Nothing) Handles Me.MouseEnter, Me.Loaded, Me.IsEnabledChanged
  83. Try
  84. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso Not False.Equals(e) Then '防止默认属性变更触发动画,若强制不执行动画,则 e 为 False
  85. Select Case ColorType
  86. Case ColorState.Black
  87. If IsMouseDown Then
  88. '按下
  89. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  90. ElseIf IsMouseOver Then
  91. '指向
  92. AniStart({
  93. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  94. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  95. }, "MyIconTextButton Checked " & Uuid)
  96. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  97. ElseIf IsEnabled Then
  98. '正常
  99. AniStart({
  100. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush1", AnimationTimeOfMouseOut),
  101. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush1", AnimationTimeOfMouseOut)
  102. }, "MyIconTextButton Checked " & Uuid)
  103. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  104. Else
  105. '禁用
  106. AniStart({
  107. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  108. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  109. }, "MyIconTextButton Checked " & Uuid)
  110. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  111. End If
  112. Case ColorState.Highlight
  113. If IsMouseDown Then
  114. '按下
  115. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  116. ElseIf IsMouseOver Then
  117. '指向
  118. AniStart({
  119. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  120. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  121. }, "MyIconTextButton Checked " & Uuid)
  122. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  123. ElseIf IsEnabled Then
  124. '正常
  125. AniStart({
  126. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseOut),
  127. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseOut)
  128. }, "MyIconTextButton Checked " & Uuid)
  129. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  130. Else
  131. '禁用
  132. AniStart({
  133. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  134. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  135. }, "MyIconTextButton Checked " & Uuid)
  136. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  137. End If
  138. End Select
  139. Else
  140. '不使用动画
  141. AniStop("MyIconTextButton Checked " & Uuid)
  142. AniStop("MyIconTextButton Color " & Uuid)
  143. Select Case ColorType
  144. Case ColorState.Black
  145. Background = ColorSemiTransparent
  146. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  147. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  148. Case ColorState.Highlight
  149. Background = ColorSemiTransparent
  150. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  151. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  152. End Select
  153. End If
  154. Catch ex As Exception
  155. Log(ex, "刷新带图标按钮颜色出错")
  156. End Try
  157. End Sub
  158. End Class