MyIconTextButton.xaml.vb 9.8 KB

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