MyIconTextButton.xaml.vb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Public Property EventType As String
  70. Get
  71. Return GetValue(EventTypeProperty)
  72. End Get
  73. Set(value As String)
  74. SetValue(EventTypeProperty, value)
  75. End Set
  76. End Property
  77. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyIconTextButton), New PropertyMetadata(Nothing))
  78. Public Property EventData As String
  79. Get
  80. Return GetValue(EventDataProperty)
  81. End Get
  82. Set(value As String)
  83. SetValue(EventDataProperty, value)
  84. End Set
  85. End Property
  86. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyIconTextButton), New PropertyMetadata(Nothing))
  87. '动画
  88. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  89. Private Const AnimationTimeOfMouseOut As Integer = 150 '鼠标移出动画长度
  90. Private Sub RefreshColor(Optional obj = Nothing, Optional e = Nothing) Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded, Me.IsEnabledChanged
  91. Try
  92. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso Not False.Equals(e) Then '防止默认属性变更触发动画,若强制不执行动画,则 e 为 False
  93. Select Case ColorType
  94. Case ColorState.Black
  95. If IsMouseDown Then
  96. '按下
  97. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  98. ElseIf IsMouseOver Then
  99. '指向
  100. AniStart({
  101. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  102. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  103. }, "MyIconTextButton Checked " & Uuid)
  104. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  105. ElseIf IsEnabled Then
  106. '正常
  107. AniStart({
  108. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush1", AnimationTimeOfMouseOut),
  109. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush1", AnimationTimeOfMouseOut)
  110. }, "MyIconTextButton Checked " & Uuid)
  111. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  112. Else
  113. '禁用
  114. AniStart({
  115. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  116. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  117. }, "MyIconTextButton Checked " & Uuid)
  118. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  119. End If
  120. Case ColorState.Highlight
  121. If IsMouseDown Then
  122. '按下
  123. AniStart(AaColor(Me, BackgroundProperty, "ColorBrush6", 70), "MyIconTextButton Color " & Uuid)
  124. ElseIf IsMouseOver Then
  125. '指向
  126. AniStart({
  127. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseIn),
  128. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn)
  129. }, "MyIconTextButton Checked " & Uuid)
  130. AniStart(AaColor(Me, BackgroundProperty, "ColorBrushBg1", AnimationTimeOfMouseIn), "MyIconTextButton Color " & Uuid)
  131. ElseIf IsEnabled Then
  132. '正常
  133. AniStart({
  134. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrush3", AnimationTimeOfMouseOut),
  135. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseOut)
  136. }, "MyIconTextButton Checked " & Uuid)
  137. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  138. Else
  139. '禁用
  140. AniStart({
  141. AaColor(ShapeLogo, Shapes.Path.FillProperty, "ColorBrushGray5", 100),
  142. AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrushGray5", 100)
  143. }, "MyIconTextButton Checked " & Uuid)
  144. AniStart(AaColor(Me, BackgroundProperty, ColorSemiTransparent - Background, AnimationTimeOfMouseOut), "MyIconTextButton Color " & Uuid)
  145. End If
  146. End Select
  147. Else
  148. '不使用动画
  149. AniStop("MyIconTextButton Checked " & Uuid)
  150. AniStop("MyIconTextButton Color " & Uuid)
  151. Select Case ColorType
  152. Case ColorState.Black
  153. Background = ColorSemiTransparent
  154. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  155. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  156. Case ColorState.Highlight
  157. Background = ColorSemiTransparent
  158. ShapeLogo.SetResourceReference(Shapes.Path.FillProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  159. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush3", "ColorBrushGray5"))
  160. End Select
  161. End If
  162. Catch ex As Exception
  163. Log(ex, "刷新按钮颜色出错")
  164. End Try
  165. End Sub
  166. End Class