1
0

MyIconTextButton.xaml.vb 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. Public Property ColorType As ColorState
  45. Get
  46. Return GetValue(ColorTypeProperty)
  47. End Get
  48. Set(value As ColorState)
  49. If ColorType = value Then Return
  50. SetValue(ColorTypeProperty, value)
  51. RefreshColor()
  52. End Set
  53. End Property '颜色类别
  54. Public Shared ReadOnly ColorTypeProperty As DependencyProperty =
  55. DependencyProperty.Register("ColorType", GetType(ColorState), GetType(MyIconTextButton), New PropertyMetadata(ColorState.Black))
  56. '点击事件
  57. Public Event Click(sender As Object, e As RouteEventArgs)
  58. Private IsMouseDown As Boolean = False
  59. Private Sub MyIconTextButton_MouseUp() Handles Me.MouseLeftButtonUp
  60. If Not IsMouseDown Then Exit Sub
  61. Log("[Control] 按下带图标按钮:" & Text)
  62. IsMouseDown = False
  63. RaiseEvent Click(Me, New RouteEventArgs(True))
  64. ModEvent.TryStartEvent(EventType, EventData)
  65. RefreshColor()
  66. End Sub
  67. Private Sub MyIconTextButton_MouseDown() Handles Me.MouseLeftButtonDown
  68. IsMouseDown = True
  69. RefreshColor()
  70. End Sub
  71. Private Sub MyIconTextButton_MouseLeave() Handles Me.MouseLeave
  72. IsMouseDown = False
  73. RefreshColor()
  74. End Sub
  75. Public Property EventType As String
  76. Get
  77. Return GetValue(EventTypeProperty)
  78. End Get
  79. Set(value As String)
  80. SetValue(EventTypeProperty, value)
  81. End Set
  82. End Property
  83. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyIconTextButton), New PropertyMetadata(Nothing))
  84. Public Property EventData As String
  85. Get
  86. Return GetValue(EventDataProperty)
  87. End Get
  88. Set(value As String)
  89. SetValue(EventDataProperty, value)
  90. End Set
  91. End Property
  92. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyIconTextButton), New PropertyMetadata(Nothing))
  93. '动画
  94. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  95. Private Const AnimationTimeOfMouseOut As Integer = 150 '鼠标移出动画长度
  96. Private Sub RefreshColor(Optional obj = Nothing, Optional e = Nothing) Handles Me.MouseEnter, Me.Loaded, Me.IsEnabledChanged
  97. Try
  98. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso Not False.Equals(e) Then '防止默认属性变更触发动画,若强制不执行动画,则 e 为 False
  99. Select Case ColorType
  100. Case ColorState.Black
  101. If IsMouseDown Then
  102. '按下
  103. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  104. ElseIf IsMouseOver Then
  105. '指向
  106. AniStart({
  107. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  108. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  109. }, "MyIconTextButton Checked " & Uuid)
  110. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  111. ElseIf IsEnabled Then
  112. '正常
  113. AniStart({
  114. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush1", AnimationTimeOfMouseOut),
  115. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush1", AnimationTimeOfMouseOut)
  116. }, "MyIconTextButton Checked " & Uuid)
  117. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  118. Else
  119. '禁用
  120. AniStart({
  121. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  122. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  123. }, "MyIconTextButton Checked " & Uuid)
  124. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  125. End If
  126. Case ColorState.Highlight
  127. If IsMouseDown Then
  128. '按下
  129. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  130. ElseIf IsMouseOver Then
  131. '指向
  132. AniStart({
  133. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  134. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  135. }, "MyIconTextButton Checked " & Uuid)
  136. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  137. ElseIf IsEnabled Then
  138. '正常
  139. AniStart({
  140. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseOut),
  141. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseOut)
  142. }, "MyIconTextButton Checked " & Uuid)
  143. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  144. Else
  145. '禁用
  146. AniStart({
  147. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  148. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  149. }, "MyIconTextButton Checked " & Uuid)
  150. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  151. End If
  152. End Select
  153. Else
  154. '不使用动画
  155. AniStop("MyIconTextButton Checked " & Uuid)
  156. AniStop("MyIconTextButton Color " & Uuid)
  157. Select Case ColorType
  158. Case ColorState.Black
  159. Background = ColorSemiTransparent
  160. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  161. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  162. Case ColorState.Highlight
  163. Background = ColorSemiTransparent
  164. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  165. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  166. End Select
  167. End If
  168. Catch ex As Exception
  169. Log(ex, "刷新带图标按钮颜色出错")
  170. End Try
  171. End Sub
  172. End Class