MyIconTextButton.xaml.vb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.TryStartEvent(EventType, EventData)
  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. '动画
  101. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  102. Private Const AnimationTimeOfMouseOut As Integer = 150 '鼠标移出动画长度
  103. Private Sub RefreshColor(Optional obj = Nothing, Optional e = Nothing) Handles Me.MouseEnter, Me.Loaded, Me.IsEnabledChanged
  104. Try
  105. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso Not False.Equals(e) Then '防止默认属性变更触发动画,若强制不执行动画,则 e 为 False
  106. Select Case ColorType
  107. Case ColorState.Black
  108. If IsMouseDown Then
  109. '按下
  110. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  111. ElseIf IsMouseOver Then
  112. '指向
  113. AniStart({
  114. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  115. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  116. }, "MyIconTextButton Checked " & Uuid)
  117. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  118. ElseIf IsEnabled Then
  119. '正常
  120. AniStart({
  121. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush1", AnimationTimeOfMouseOut),
  122. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush1", AnimationTimeOfMouseOut)
  123. }, "MyIconTextButton Checked " & Uuid)
  124. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  125. Else
  126. '禁用
  127. AniStart({
  128. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  129. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  130. }, "MyIconTextButton Checked " & Uuid)
  131. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  132. End If
  133. Case ColorState.Highlight
  134. If IsMouseDown Then
  135. '按下
  136. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  137. ElseIf IsMouseOver Then
  138. '指向
  139. AniStart({
  140. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  141. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  142. }, "MyIconTextButton Checked " & Uuid)
  143. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  144. ElseIf IsEnabled Then
  145. '正常
  146. AniStart({
  147. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseOut),
  148. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseOut)
  149. }, "MyIconTextButton Checked " & Uuid)
  150. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  151. Else
  152. '禁用
  153. AniStart({
  154. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  155. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  156. }, "MyIconTextButton Checked " & Uuid)
  157. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  158. End If
  159. End Select
  160. Else
  161. '不使用动画
  162. AniStop("MyIconTextButton Checked " & Uuid)
  163. AniStop("MyIconTextButton Color " & Uuid)
  164. Select Case ColorType
  165. Case ColorState.Black
  166. Background = ColorSemiTransparent
  167. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  168. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  169. Case ColorState.Highlight
  170. Background = ColorSemiTransparent
  171. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  172. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  173. End Select
  174. End If
  175. Catch ex As Exception
  176. Log(ex, "刷新带图标按钮颜色出错")
  177. End Try
  178. End Sub
  179. End Class