Imports System.ComponentModel
Public Class CairoLabel
Inherits Label
Private _opacity As Double = 1
Public Sub New()
MyBase.SetStyle(ControlStyles.UserPaint, True)
MyBase.SetStyle(ControlStyles.ResizeRedraw, True)
MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
MyBase.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
MyBase.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub
<Browsable(True), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(1.0R), TypeConverter(GetType(OpacityConverter))>
<RefreshProperties(RefreshProperties.All), Description("Gets or set the opacity percentage of the control."), Category("Cairo")>
Public Overridable Property Opacity() As Double
Get
Return _opacity
End Get
Set(value As Double)
_opacity = value
Dim c As Color = Me.BackColor
Dim alpha As Byte = CalcualteOpacity(value)
Me.BackColor = UpdateControlColor(c, alpha)
Me.Invalidate()
End Set
End Property
Protected Overrides Sub OnBackColorChanged(e As EventArgs)
MyBase.OnBackColorChanged(e)
Dim c As Color = Me.BackColor
Dim alpha As Byte = CalcualteOpacity(_opacity)
Me.BackColor = UpdateControlColor(c, alpha)
Me.Invalidate()
End Sub
Private Function UpdateControlColor(source As Color, alpha As Byte) As Color
alpha = Byte.MaxValue - alpha
Return Color.FromArgb(CInt((1 - (alpha / Byte.MaxValue)) * Byte.MaxValue), source)
End Function
Private Function CalcualteOpacity(value As Double) As Byte
Return CByte((If(value > 1, 1, If(value < 0, 0, value))) * 255)
End Function
End Class