teilweise transparentes label auf picturebox?

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von vanitas-mundi.

    teilweise transparentes label auf picturebox?

    hallo alle zusammen!

    gibt es eine möglichkeit ein label über eine picturebox zu setzen und anschließend die hintergrundfarbe des labels auf z.b. grau zu stellen und dennoch die picturebox leicht durchschimmern zu lassen?

    da sowohl die größe und der inhalt der picturebox als auch die größe und text des labels variabel sein sollte ist es leider nicht möglich den text inkl. durchschimmerndem hintergrund schon vorher auf das bild zu "malen" mit hilfe eines bildbearbeitungsprogramm.

    LG
    Hi

    Leichte Transparenz kannst du über Color.FromArgb(Alpha, Rot, Grün, Blau) erreichen. Eine geschicktere Möglichkeit wäre, wenn du statt dem Label die DrawString und FillRectangle methoden von PictureBoxN.CreateGraphics verwenden würdest...

    Allerdings wird das Label trotzdem nur den Form hintergrund(bzw. den Containerhintergrund) als transparenz machen.

    Gruß
    ~blaze~
    Hallo!

    Von ~blaze~ inspiriert habe ich schnell einmal ein nettes OpacityLabel
    gebastelt, welches ich Interessierten hier gerne zur Verfügung stellen
    wollte:

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Option Infer On
    4. Namespace Windows.Forms
    5. #Region " --------------->> Klasse <ClassName> "
    6. Public Class OpacityLabelControl
    7. Inherits ScrollableControl
    8. #Region " --------------->> Enumerationen der Klasse "
    9. '{Enumerationen}
    10. #End Region '{Enumerationen der Klasse}
    11. #Region " --------------->> Eigenschaften der Klasse "
    12. '{Eigenschaften}
    13. Public Event FadeInComplete(ByVal sender As Object, ByVal e As EventArgs)
    14. Public Event FadeOutComplete(ByVal sender As Object, ByVal e As EventArgs)
    15. Private _DoFadeInAndOut As Boolean = False
    16. Private WithEvents _StayTimer As New Timer
    17. Private _StayDelayTime As Int32
    18. Private _FadeOutDelayTime As Int32
    19. Private _FadeOpacityDelta As Int32
    20. Private _FadeCount As Int32 = 10
    21. Private _CurrentFadeCount As Int32
    22. Private _ShadowEddect As Boolean = False
    23. Private _ForeColor2 As Color = System.Drawing.SystemColors.ControlText
    24. Private WithEvents _Timer As New Timer
    25. Private _Opacity As Int32 = 100
    26. Private _GradiantMode As Drawing2D.LinearGradientMode _
    27. = Drawing2D.LinearGradientMode.BackwardDiagonal
    28. #End Region '{Eigenschaften der Klasse}
    29. #Region " --------------->> Konstruktor und Destruktor der Klasse "
    30. '{Konstruktor/ Destruktor}
    31. Public Sub New()
    32. MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    33. Me.BackColor = Color.Transparent
    34. Me.Opacity = 100
    35. End Sub
    36. #End Region '{Konstruktor und Destruktor der Klasse}
    37. #Region " --------------->> Zugriffsmethoden der Klasse "
    38. '{Zugrifsmethoden}
    39. Public Property ShadowEddect() As Boolean
    40. Get
    41. Return _ShadowEddect
    42. End Get
    43. Set(ByVal value As Boolean)
    44. _ShadowEddect = value
    45. End Set
    46. End Property
    47. Public Property GradiantMode() As Drawing2D.LinearGradientMode
    48. Get
    49. Return _GradiantMode
    50. End Get
    51. Set(ByVal value As Drawing2D.LinearGradientMode)
    52. _GradiantMode = value
    53. Me.Refresh()
    54. End Set
    55. End Property
    56. Public Property ForeColor2() As Color
    57. Get
    58. Return _ForeColor2
    59. End Get
    60. Set(ByVal value As Color)
    61. _ForeColor2 = value
    62. Me.Refresh()
    63. End Set
    64. End Property
    65. Public Property Opacity() As Int32
    66. Get
    67. Return _Opacity
    68. End Get
    69. Set(ByVal value As Int32)
    70. Select Case value
    71. Case Is < 0
    72. _Opacity = 0
    73. Case 0 To 100
    74. _Opacity = value
    75. Case Is > 100
    76. _Opacity = 100
    77. End Select
    78. Me.Refresh()
    79. End Set
    80. End Property
    81. #End Region '{Zugriffsmethoden der Klasse}
    82. #Region " --------------->> Ereignismethoden der Klasse "
    83. '{Ereignismethoden}
    84. Private Sub OpacityLabel_Paint _
    85. (ByVal sender As Object _
    86. , ByVal e As System.Windows.Forms.PaintEventArgs) _
    87. Handles Me.Paint
    88. e.Graphics.SmoothingMode _
    89. = Drawing.Drawing2D.SmoothingMode.AntiAlias
    90. e.Graphics.TextRenderingHint _
    91. = Drawing.Text.TextRenderingHint.ClearTypeGridFit
    92. Dim opacity As Int32 = CType((255 * _Opacity) / 100, Int32)
    93. Dim size = e.Graphics.MeasureString(Me.Text, Me.Font)
    94. Dim rect As New Rectangle(0, 0 _
    95. , CType(size.Width - 1, Int32) _
    96. , CType(size.Height - 1, Int32))
    97. Me.Size = New Size(CType(size.Width + 1, Int32) _
    98. , CType(size.Height + 1, Int32))
    99. If Me.ShadowEddect Then
    100. Dim shadowColor As Color = Color.FromArgb _
    101. (opacity, BCW.etc.Functions.GenerateBrightColor(Me.ForeColor, 150))
    102. Dim shadowColor2 As Color = Color.FromArgb _
    103. (opacity, BCW.etc.Functions.GenerateBrightColor(Me.ForeColor2, 150))
    104. Using shadowBrush As New System.Drawing.Drawing2D.LinearGradientBrush _
    105. (rect _
    106. , Color.FromArgb(opacity, shadowColor) _
    107. , Color.FromArgb(opacity, shadowColor2) _
    108. , Me.GradiantMode)
    109. e.Graphics.DrawString(Me.Text, Me.Font, shadowBrush, 1, 1)
    110. End Using
    111. End If
    112. Using b As New System.Drawing.Drawing2D.LinearGradientBrush _
    113. (rect _
    114. , Color.FromArgb(opacity, Me.ForeColor) _
    115. , Color.FromArgb(opacity, Me.ForeColor2) _
    116. , Me.GradiantMode)
    117. e.Graphics.DrawString(Me.Text, Me.Font, b, 0, 0)
    118. End Using
    119. End Sub
    120. Private Sub OpacityLabel_FadeInComplete _
    121. (ByVal sender As Object _
    122. , ByVal e As System.EventArgs) _
    123. Handles Me.FadeInComplete
    124. If Not _DoFadeInAndOut Then Exit Sub
    125. _StayTimer.Interval = _StayDelayTime
    126. _StayTimer.Start()
    127. End Sub
    128. Private Sub onFadeOutComplete _
    129. (ByVal sender As Object _
    130. , ByVal e As System.EventArgs) _
    131. Handles Me.FadeOutComplete
    132. _DoFadeInAndOut = False
    133. End Sub
    134. Private Sub _StayTimer_Tick _
    135. (ByVal sender As Object _
    136. , ByVal e As System.EventArgs) _
    137. Handles _StayTimer.Tick
    138. _StayTimer.Stop()
    139. Me.FadeOut(_FadeOutDelayTime)
    140. End Sub
    141. Private Sub _Timer_Tick _
    142. (ByVal sender As Object _
    143. , ByVal e As System.EventArgs) _
    144. Handles _Timer.Tick
    145. _CurrentFadeCount -= 1
    146. Me.Opacity += _FadeOpacityDelta
    147. If _CurrentFadeCount = 0 Then
    148. _Timer.Stop()
    149. If Me.Opacity > 50 Then
    150. Me.Opacity = 100
    151. RaiseEvent FadeInComplete(Me, New EventArgs)
    152. Else
    153. Me.Opacity = 0
    154. RaiseEvent FadeOutComplete(Me, New EventArgs)
    155. End If
    156. End If
    157. End Sub
    158. #End Region
    159. #Region " --------------->> Private Methoden der Klasse "
    160. '{Private Methoden}
    161. #End Region '{Private Methoden der Klasse}
    162. #Region " --------------->> Öffentliche Methoden der Klasse "
    163. '{Öffentliche Methoden}
    164. Public Sub FadeIn(ByVal delayTime As Int32)
    165. Me.Opacity = 0
    166. _Timer.Interval = delayTime \ _FadeCount
    167. _FadeOpacityDelta = (100 \ _FadeCount)
    168. _CurrentFadeCount = _FadeCount
    169. _Timer.Start()
    170. End Sub
    171. Public Sub FadeOut(ByVal delayTime As Int32)
    172. Me.Opacity = 100
    173. _Timer.Interval = delayTime \ _FadeCount
    174. _FadeOpacityDelta = (100 \ _FadeCount) * -1
    175. _CurrentFadeCount = _FadeCount
    176. _Timer.Start()
    177. End Sub
    178. Public Sub FadeInAndOut _
    179. (ByVal fadeInDelayTime As Int32 _
    180. , ByVal stayDelayTime As Int32 _
    181. , ByVal fadeOutDelayTime As Int32)
    182. _StayDelayTime = stayDelayTime
    183. _FadeOutDelayTime = fadeOutDelayTime
    184. _DoFadeInAndOut = True
    185. Me.FadeIn(fadeInDelayTime)
    186. End Sub
    187. #End Region 'Öffentliche Methoden der Klasse}
    188. End Class
    189. #End Region '{Klasse <ClassName>}
    190. End Namespace


    Über die Opacity-Eigenschaft kann die Transparenz eingestellt werden, wobei 0 komplette
    Tranzparenz bedeuetet und 100 voll sichtbar.

    Viel Spaß damit :)