• 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.
  • Posts

    • Switch to another OS if you're into tinfoil hat mentality, or just uninstall the app. Oh, wait, even after uninstalling it, can Microsoft still be looking at everything? We'll never know, so, yeah, that's that. If you use Windows, you need to live with those options.
    • Minecraft Chase the Skies update and Vibrant Visuals graphics overhaul lands next week by Pulasthi Ariyasinghe The second major game update of the year for Minecraft is almost here. Mojang today announced that its next Game Drop, Chase the Skies, is landing on June 17. Alongside it, the highly anticipated graphics overhaul, dubbed Vibrant Visuals, will be available too, making the day a rather big occasion for Minecraft fans. The Chase the Skies update has a large number of gameplay features and content, but one of the biggest is the expansion of the Ghast. Now, players will be able to find Happy Ghasts as a tameable mob, letting up to five players ride them on journeys across the skies. To get one, players will have to find small, dehydrated Ghasts near fossil structures in the Nether, then bring it to the overworld and feed it plenty of water. It is also possible to craft dried Ghasts from Nether materials. Other features of this update include a handy player locator bar, better leads functionality, craftable saddles, a new music disc, and more. As for the Vibrant Visuals upgrade, Mojang is touting a visual refresh that lets players witness how lights and shadows affect the Minecraft world. "Fly high with your happy ghast to watch the sunrise reflect on the rippling ocean, make art with shifting shadows, descend into dark depths, or simply sit back and enjoy the vivid beauty of each biome," says the studio. "With updated in-game visuals you’ll see how light and shadow transform the Overworld – water reflects, forests become shaded, fog drifts, and waterfalls glimmer in the light." The currently supported devices for Vibrant Visuals include Xbox Series X|S, Xbox One, PlayStation 4, PlayStation 5, PC, as well as Android (Android: Adreno 640, Mali-G68, Mali-G77, or Xclipse 530 or higher) and iOS (A12 or M1 or higher.)   Unfortunately, the Vibrant Visuals upgrade will only be available on the Bedrock Edition of Minecraft, at least for now. Mojang has said that it plans to bring the visual overhaul to the Java Edition of Minecraft at a later date, but no information or release window has been announced just yet.
    • Once people get use to the square-ish case, they'll probably switch the display to square as well.
    • Why would you think it's not enabled all the time? Because they said so?
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      560
    2. 2
      ATLien_0
      255
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      158
    5. 5
      Michael Scrip
      110
  • Tell a friend

    Love Neowin? Tell a friend!