• 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

    • My son is in Monaco right now, and I was checking his location in Apple's Find My app. I noticed that Prince Albert's Palace was blurred out on the satellite imagery in both Find My and Apple Maps. I checked Google Maps, and the palace wasn't blurred there. Does Apple have some kind of process where property owners can request that their homes be blurred on Apple Maps?  
    • No, it was THAT ugly and I’d rather forget it completely existed.
    • There is a lot of reasons not to use Edge but faster fixes and security updates is not one of them.
    • Can't reproduce. I installed Edge, went to neowin.net > accepted the cookie consent > used menu to go to forums, everything loads and I can browse around the forums. If you can't interact with the dialog on the forums for some reason, go to the main site and accept the cookie consent there? It is true that the site will not function properly until the cookie consent is accepted or rejected,. it's a legal requirement and I also know that certain VPN/ad blockers block it, which is a user related issue and not a neowin.net problem.   This is not our cookie consent dialog. Gotta love browser hijacking... /s Edit: this may be what Californians see, I will confirm with our consent provider.
    • Google Chrome 149.0.7827.115 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      491
    2. 2
      PsYcHoKiLLa
      170
    3. 3
      +Edouard
      164
    4. 4
      Steven P.
      85
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!