Tech Off Thread

19 posts

Label with transparent background in Windows Forms?

Back to Forum: Tech Off
  • User profile image
    qwert231

    I would like to have text that is on the screen but has a transparent background. Is this possible? I tried to set background color to transparent, but that doesn't seem to work.

  • User profile image
    littleguru

    All .NET win form controls are own windows. That's why it's impossible to have a transparent background.

    Setting the background to transparent only takes the background color of the parent window (in most cases a form).

    It's not supported by the .NET win form controls!

  • User profile image
    qwert231

    Hmm.... that is lame....

    Is there anything I can do? Other than writing my own override of the paint command?

  • User profile image
    Frank Hileman

    There are some solutions. Searching the msdn forums:
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=672353&SiteID=1
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=60677&SiteID=1

    There are also components that let you use label-like objects that are translucent:
    http://www.vgdotnet.com

  • User profile image
    JeremyJ

    label.BackColor = System.Drawing.Color.Transparent;

  • User profile image
    TommyCarlier

    True transparent Win32-based controls are not possible, Win32 is not designed to support this. But partial transparency can be simulated.

    At work (and also at home), I use the free Krypton Toolkit, where the controls support a transparent or translucent background.

    Besides supporting transparent backgrounds, they are also very flexible, and nice looking. It has some built-in styles (like Office 2007-look), and you can also create your own styles. And according to Phil Wright's blog (which is very cool), he's currently developing a ribbon control.

  • User profile image
    nicksun

    TommyCarlier said:

    True transparent Win32-based controls are not possible, Win32 is not designed to support this. But partial transparency can be simulated.

    At work (and also at home), I use the free Krypton Toolkit, where the controls support a transparent or translucent background.

    Besides supporting transparent backgrounds, they are also very flexible, and nice looking. It has some built-in styles (like Office 2007-look), and you can also create your own styles. And according to Phil Wright's blog (which is very cool), he's currently developing a ribbon control.

    1. By setting a Panel's BackgroundImage property and putting the Label(s) inside it
    2. By parenting the PictureBox to the Label (label.Parent = pictureBox;) 

  • User profile image
    W3bbo

    I wrote a lightweight windowless control framework (no Design Time support though), it's not too hard and you can get some good effects with it; it's how I implemented a glass-compatible Aero Wizard back button in WinForms.

    Basically define a new abstract class WindowlessControl with abstract void Paint(Graphics g), then extend the Form class to give it a Collection<WindowlessControl> then in OnPaint do foreach(WindowlessControl c in wlcCollection) c.Paint( g );

    You'll need to implement hit testing and other things though.

  • User profile image
    AndyC

    W3bbo said:
    I wrote a lightweight windowless control framework (no Design Time support though), it's not too hard and you can get some good effects with it; it's how I implemented a glass-compatible Aero Wizard back button in WinForms.

    Basically define a new abstract class WindowlessControl with abstract void Paint(Graphics g), then extend the Form class to give it a Collection<WindowlessControl> then in OnPaint do foreach(WindowlessControl c in wlcCollection) c.Paint( g );

    You'll need to implement hit testing and other things though.
    Or just use WPF. In all honesty, by the time you start needing windowless controls, you're going to be much more productive switching to WPF than you'll ever be re-inventing the wheel for Windows Forms. 2 1/2 years ago (when the thread started) you'd have had cause not to consider WPF, but there's really no good reason now.

  • User profile image
    W3bbo

    AndyC said:
    W3bbo said:
    *snip*
    Or just use WPF. In all honesty, by the time you start needing windowless controls, you're going to be much more productive switching to WPF than you'll ever be re-inventing the wheel for Windows Forms. 2 1/2 years ago (when the thread started) you'd have had cause not to consider WPF, but there's really no good reason now.
    I don't know much about WPF yet, but with WPF is it easy to respect system settings and convention? I don't want a Flash-like environment where my application is unique: I want it to have the same style as the OS.

    At DDD in 2006 there was a demo of WPF back when it was called Avalon and he showed the Button control as a scalable control that was a re-implementation of the XP Luna Blue style of button.

    What about MainMenus? I'm assuming WPF comes with its own menubar class, but why not take advantage of the menu bar system built-in to Windows since...ever?

  • User profile image
    Cannot​Resolve​Symbol

    W3bbo said:
    AndyC said:
    *snip*
    I don't know much about WPF yet, but with WPF is it easy to respect system settings and convention? I don't want a Flash-like environment where my application is unique: I want it to have the same style as the OS.

    At DDD in 2006 there was a demo of WPF back when it was called Avalon and he showed the Button control as a scalable control that was a re-implementation of the XP Luna Blue style of button.

    What about MainMenus? I'm assuming WPF comes with its own menubar class, but why not take advantage of the menu bar system built-in to Windows since...ever?
    The default WPF controls take their style from the system theme; they fit in as well as they can (since it's WPF, you get the usual WPF issues like text blurriness and such).

    They don't actually use the system controls, though, as that would defeat the point of WPF (which is to allow every control to be scalable, themeable, etc.)

  • User profile image
    AndyC

    Yes, it's very easy to respect system settings, that's pretty much the default look of all controls. And, of course, you can easily get access to system colours and so on to allow your custom controls to adapt to the OS settings.

    As to the menu, yes WPF has it's own. It's much more flexible than the old User32 one, since each menu item can contain whatever WPF controls you like, which makes creating much more rich menu functionality really easy. Replicating the drop down menus in Office 2007 (such as the font picker or colour selector) is trivial, compared with a significant amount of coding effort required using more traditional technologies.

  • User profile image
    Sven Groot

    Not to mention that, like everything else in WPF, menus can be databound. So it's trivial to make something like a recent items menu with only a couple of lines of XAML, in a much cleaner fashion than WinForms or Win32 could ever achieve.

    EDIT: One major problem with WPF is that the Luna and Aero styles are hard coded into WPF. If you use a different visual style, like the Zune one MS released earlier or an unofficial one with a hacked uxtheme.dll, WPF will revert to the Windows classic style controls because it has no matching style set for the theme you're using. WPF does not use the visual style APIs to draw its controls (nor can it, because they don't provide the functionality WPF needs).

  • User profile image
    W3bbo

    Sven Groot said:
    Not to mention that, like everything else in WPF, menus can be databound. So it's trivial to make something like a recent items menu with only a couple of lines of XAML, in a much cleaner fashion than WinForms or Win32 could ever achieve.

    EDIT: One major problem with WPF is that the Luna and Aero styles are hard coded into WPF. If you use a different visual style, like the Zune one MS released earlier or an unofficial one with a hacked uxtheme.dll, WPF will revert to the Windows classic style controls because it has no matching style set for the theme you're using. WPF does not use the visual style APIs to draw its controls (nor can it, because they don't provide the functionality WPF needs).
    Really? That's a killer disappointment. Has anyone attempted to make a style for WPF that uses the VisualStyle APIs and makes up for what's missing?

  • User profile image
    AndyC

    W3bbo said:
    Sven Groot said:
    *snip*
    Really? That's a killer disappointment. Has anyone attempted to make a style for WPF that uses the VisualStyle APIs and makes up for what's missing?
    Why? You'd just end up with horrible scaled bitmaps when running on a higher DPI. Of course, there is nothing to stop you doing that in WPF, but it'd look proper ugly if you ask me.

  • User profile image
    ploe

    I needed to do this will a Panel. Maybe it'll work for a Label too...

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            if (PaintMode == PanelPaintMode.Transparent)
                createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

  • User profile image
    W3bbo

    AndyC said:
    W3bbo said:
    *snip*
    Why? You'd just end up with horrible scaled bitmaps when running on a higher DPI. Of course, there is nothing to stop you doing that in WPF, but it'd look proper ugly if you ask me.
    You don't get scaled bitmaps with WinForms at high DPI settings. A well-implemented VisualStyle-compatible WPF app wouldn't either.

  • User profile image
    AndyC

    W3bbo said:
    AndyC said:
    *snip*
    You don't get scaled bitmaps with WinForms at high DPI settings. A well-implemented VisualStyle-compatible WPF app wouldn't either.
    But WinForms is considerably more limited in how you can draw controls, you can't put a button at an angle, or apply a 3D transform to a control or do a VisualBrush style effect etc. Unless you can turn the Windows visual style into a vector drawing (non-trivial) you're going to end up converting it into bitmaps and hoping for the best.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.