Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hey CP,
looking for label forecolor opacity (not transparent)...
any link (or code) on how to builed such control class or handle exist one?
thanks a head...
oron :-)
Posted
Comments
Ralf Meier 1-Jun-15 3:47am    
ForeColor-Opacity ...?
Please explain what you mean ...
oronsultan 1-Jun-15 4:18am    
ooooopsssss...
i meant text opacity.
my bad...
i need the text of the label will be able to see threw it.

Hi,
I made some experiments ... and now I have a solution for you.
Perhaps you do some Changes - but it works like you asked ...

VB
Public Class customLabel
    Inherits Label

    Sub New()
        Me.SetStyle(ControlStyles.Opaque, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)

         Font = New System.Drawing.Font("Microsoft Sans Serif", 28.2!)
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20  ' Turn on WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property

    Property Opacity As Integer
        Get
            Return myOpacity
        End Get
        Set(value As Integer)
            If value >= 0 And value <= 255 Then myOpacity = value
            Invalidate()
        End Set
    End Property
    Private myOpacity As Integer = 0

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        Dim myBitmap As New Bitmap(Me.Width, Me.Height)
        Dim gr As Graphics = Graphics.FromImage(myBitmap)

        Dim pBackColor As Color = myBackColor
        If myBackColor = Color.Transparent Then
            If myParent IsNot Nothing Then pBackColor = myParent.BackColor
        End If

        Dim pe As New System.Windows.Forms.PaintEventArgs(gr, e.ClipRectangle)
        MyBase.OnPaint(pe)

        For y As Integer = 0 To Height - 1
            For x As Integer = 0 To Width - 1
                Dim PixelColor As Color = myBitmap.GetPixel(x, y)

                If PixelColor.A <> 0 Then
                    myBitmap.SetPixel(x, y, Color.FromArgb(myOpacity, PixelColor))
                Else
                    myBitmap.SetPixel(x, y, Color.FromArgb(255, pBackColor))
                End If
            Next x
        Next y

        e.Graphics.DrawImage(myBitmap, 0, 0)

    End Sub

    Private Sub me_HandleCreated(sender As Object, e As System.EventArgs) Handles Me.HandleCreated
        myParent = Parent
        If myParent IsNot Nothing Then myParent_Enabled = myParent.Enabled
    End Sub
    Private WithEvents myParent As Control

    Overrides Property BackColor As Color
        Get
            Return myBackColor
        End Get
        Set(ByVal value As Color)
            myBackColor = value
            Me.Invalidate()
        End Set
    End Property

    Private myBackColor As Color = Color.Blue

End Class
 
Share this answer
 
v2
Comments
oronsultan 1-Jun-15 13:30pm    
First of all: B-R-A-V-O!!!! and thanks a lot for this great control, i'm sure gonna use it on my app...
great class, simple, classic and straight to the point....
if i may, i have to ask how come when i set the customeLabel background to transparent it turns white?
Ralf Meier 2-Jun-15 0:22am    
Of course you are allowed to ask.
As I have written - the control is not complete.
In case you Chose "Transparent" as Backcolor it is nescessary to read the Backcolor-Property from the parent.
I have added it to the code above ...

If this solution is what you searched please rate it and accept it formally.
Labels ForeColor property has the type of Color...Color class has built in capabilities of alpha channel handling...
So instead of setting ForeColor like:
C#
Label1.ForeColor = Color.Red;

Use FormArgb
C#
Label1.ForeColor = Color.FromArgb(150, Color.Red);

And you will have a transparent color...(adjust the alpha value to your needs)
https://msdn.microsoft.com/en-us/library/1hstcth9%28v=vs.110%29.aspx[^]
 
Share this answer
 
Comments
CHill60 1-Jun-15 4:46am    
+5
Kornfeld Eliyahu Peter 1-Jun-15 4:55am    
Thank you...
oronsultan 1-Jun-15 5:27am    
trye it your way... want good :-(
lbl3.ForeColor = Color.FromArgb(126, Color.Black)
i changed the alpha value, tryed all the alphe area (0-250).
its seems it aint got any effect on the label.
maybe somthing in the label property is wrong?
Kornfeld Eliyahu Peter 1-Jun-15 5:50am    
You right - WinForm controls from Microsoft do not support transparency...
You have two options:
1. Move to WPF
2. Create your own WinForm control
Ralf Meier 1-Jun-15 5:33am    
I can't imagine that this is the solution.
That works for BackColor if the Control itself is set to Opacity-Mode. But for ForeColor ... if you make the ForeColor 'transparent' then you will see the Control-Background (in this case the BackColor) through it ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900