MyIconTextButton.xaml.vb 9.4 KB

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