• 0

[VB.NET] Label Opacity


Question

7 answers to this question

Recommended Posts

  • 0

Have you tried setting the BackColor and ForeColor to something with an alpha value <255? Long shot but it might work, although I suspect that the alpha channel will get ignored.

Alternatively, what about using The "Transparent/Translucent" label control from The Code Project. Its unlicenced and you have free access to the source so you can make any necessary adjustments.

If you don't like either of those or they don't work, you could try just creating a class that inherits System.Windows.Forms.Label and override the OnPaint method to draw the label with opacity.

  • 0

the simple answer is: no, if you are using Windows Forms. yes, if you are using Windows Presentation Foundation.

the more complicated answer with Windows Forms is that it is drawn top-down, not bottom-up. you can't set transparency of a label over another control because the control on the bottom won't necessarily be drawn first... or at all. this is not true for top level windows. if you change the opacity of a label, some controls... like the Form itself, will fake it with some trickery. more fundamentally, WinForms is a wrapper for the Win32 API and that has limitations.. like alpha transparency for non top-level windows. if you see another program draw with control-level opacity, they are using some custom drawing code to make it happen - which can get very complex.

the Windows Presentation Foundation, on the other hand, is a wrapper for the DirectX APIs, which support full transparency and are drawn bottom-up. if you want to get fancy with your uis, use WPF.

  • 0

For WPF, yeah it is pretty simple. For Winforms, make your own control that overrides OnPaint and OnPaintBackground appropriately. Here's something that I've used once or twice. Credit to Rick Strahl for C# version at http://www.west-wind.com/WebLog/posts/247977.aspx

Imports System
Imports System.Drawing
Imports System.Windows.Forms

' &lt;summary&gt;
' A label that can be transparent.
' &lt;/summary&gt;
Public Class TransparentLabel
    Inherits Control
    ' &lt;summary&gt;
    ' Creates a new "TransparentLabel" instance.
    ' &lt;/summary&gt;
    Public Sub New()
        TabStop = False
        Dim transparencyValue As Integer = 15 '0 is all transparent, 255 is solid
        ForeColor = Color.FromArgb(transparencyValue, ForeColor.R, ForeColor.G, ForeColor.B)
    End Sub

    ' &lt;summary&gt;
    ' Gets the creation parameters.
    ' &lt;/summary&gt;
    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &amp;H20
            Return cp
        End Get
    End Property

    ' &lt;summary&gt;
    ' Paints the background.
    ' &lt;/summary&gt;
    ' &lt;param name="e"&gt;E.&lt;/param&gt;
    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
        ' do nothing
    End Sub

    ' &lt;summary&gt;
    ' Paints the control.
    ' &lt;/summary&gt;
    ' &lt;param name="e"&gt;E.&lt;/param&gt;
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Using brush As SolidBrush = New SolidBrush(ForeColor)
            e.Graphics.DrawString(Text, Font, brush, -1, 0)
        End Using
    End Sub

End Class

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.