How to tips and tricks for Microsoft Visual Studio .net

Tuesday, June 8, 2010

Transparent Windows Forms Label Control

I was recently asked how to make a windows forms Label control background transparent.

Being mostly a web app developer, I simply answered that it was easy and all you have to do is set the back color of the label to color.Transparent.

I tried it, and to my shock and horror, I was wrong. It is not that simple, because that is not the way that .net windows applications render the background of controls.

The way it works, is that the control (label in my case) will take on the back color of its parent control when you set its back color to Transparent. This is usually the form's back color, causing any controls under the label will have a nice fat rectangular shape, the same color of its containing control around the contents of the label.

Some people suggest that you can set the form's Transparency Key property to the same color as the back color of the label. All that does is make the form transparent so you can see whatever is behind the form through it, where that color is rendered on the form.

The best solution I came across is to create a new label control in your project...

http://www.doogal.co.uk/transparent.php

According the the author, there can be a problem in rendering the label in some cases, but it worked out perfectly for me.

The code is great, but is missing the how to use it part for those of you who don't know how to implement it.

To use it...

Basically you must create a new class in your application and copy the following code into your class (replace everything created for you with the code):

[c#]

 using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormsControls
{
 public class TransparentLabel : Control
 {

   public TransparentLabel()
   {
     TabStop = false;
   }

   protected override CreateParams CreateParams
   {
     get
     {
       CreateParams cp = base.CreateParams;
       cp.ExStyle |= 0x20;
       return cp;
     }
   }
 
   protected override void OnPaintBackground(PaintEventArgs e)
   {
     // do nothing
   }
 
   protected override void OnPaint(PaintEventArgs e)
   {
     using (SolidBrush brush = new SolidBrush(ForeColor))
     {
       e.Graphics.DrawString(Text, Font, brush, -1, 0);
     }
   }
 }
}


[VB]

 Imports System 
Imports System.Drawing 
Imports System.Windows.Forms 
Namespace WinFormsControls 
  Public Class TransparentLabel : Inherits Control 
    Public Sub TransparentLabel() 
      TabStop = False 
    End Sub 
    Protected Overrides ReadOnly Property CreateParams() As CreateParams 
      Get 
        Dim cp As CreateParams = MyBase.CreateParams 
        cp.ExStyle = cp.ExStyle Or &H20 
        Return cp 
      End Get 
    End Property 
    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs) 
      ' do nothing 
    End Sub 
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
      Using brush As New SolidBrush(ForeColor) 
        e.Graphics.DrawString(Text, Font, brush, -1, 0) 
      End Using 
    End Sub 
  End Class 
End Namespace 


Use the control like this:

[c#]

TransparentLabel lblTrans = new TransparentLabel();
lblTrans.Height = 35;
lblTrans.Width = 35;
lblTrans.Top = 50;
lblTrans.Left = 50;
lblTrans.Text = "l";
lblTrans.Font = new Font("Wingdings", 30);
this.Controls.Add(lblTrans);
lblTrans.Visible = true;
lblTrans.BringToFront();
[VB]
Dim lblTrans As New TransparentLabel()
lblTrans.Height = 35
lblTrans.Width = 35
lblTrans.Top = 50
lblTrans.Left = 50
lblTrans.Text = "l"
lblTrans.Font = New Font("Wingdings", 30)
Me.Controls.Add(lblTrans)
lblTrans.Visible = True
lblTrans.BringToFront()
Remember to include the following at the top of your code window to be able to use the control on your form:

[c#]
 using WinFormsControls;
[VB]
 Imports YourProjectName.WinFormsControls

If you want to use the control in multiple projects, I would suggest that you add this code to a dll and include the dll in your project.

Happy coding.

No comments:

Post a Comment