Thursday, February 07, 2008

I can see right through you... Transparent Labels in WinForms (with a VB implementation)

Rick Strahl's Web Log - Transparent Labels in WinForms

"One really annoying thing about WinForms is that there's no built in support for transparency for controls. If you want to create a label and want it to blend into any custom background colors this isn't inherently supported. Although there's a static Color.Transparent color property, when you apply to a label on a form it doesn't actually do anything - you get the same default background color (inherited fro the parent) applied instead.

...

If all else fails you can also create use raw GDI+ code to draw the label onto the form at the specified position form location. As it turns out it's not terribly difficult to do this: Here's a TransparentLabel class that does this. Didn't test this extensively (and the author warns of some possible rendering inconsistencies) but I plugged it into several of my simple scenarios of overlaying a label on top of various graphics this worked just fine.

..."

Label transparency in WinForms has bugged me for a while, one of the minor nuisance things. So when I saw the above post, I had to play with it... (and convert it to VB of course ;)

Below are a couple screenshots and the related code/class.

It is by NO means complete and the implementation is not perfect (or even really good, since I combined the transparent.designer.vb and transparent.vb into a single class for posting), but it does work and can be a good starting point for you...

image

image

Public Class TransparentLabel
Inherits System.Windows.Forms.Control

'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If
disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase
.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'TransparentLabel
'
Me.Name = "TransparentLabel"
Me.Text = "I am Transparent"
Me.Size = New System.Drawing.Size(217, 32)
Me.ResumeLayout(False)

End Sub
' Custom code starts here...
Public Sub New
()
' This call is required by the Windows Form Designer.
InitializeComponent()
Me.TabStop = False
End Sub


Protected Overrides ReadOnly Property
CreateParams() As System.Windows.Forms.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 pevent As System.Windows.Forms.PaintEventArgs)
'Do nothing...
End Sub

Protected Overrides Sub
OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Using brush As SolidBrush = New SolidBrush(ForeColor)
e.Graphics.DrawString(Text, Font, brush, -1, 0)
End Using
End Sub

End Class

1 comment:

Anonymous said...

Thanks for posting this! How can I get this to support "Padding" in the label?