Click here to Skip to main content
15,867,328 members
Articles / Desktop Programming / Windows Forms

Transparent Label .NET Control

Rate me:
Please Sign up or sign in to vote.
4.57/5 (17 votes)
9 Sep 2010CPOL1 min read 97.3K   7.5K   33   22
This article explains how to create transparent Label controls in .NET Framework, uLabelX Transparent Label Control that support border styles, Images, and parent controls with gradient backgrounds

Introduction

I think many people are having trouble creating a transparent label over a gradient background painted control.

This article illustrates an approach to showing transparent Labels in a Windows based application. uLabelX control in this library is designed to be flexible and stylish in its look. First of all, it supports border shapes, Images and parent controls with gradient backgrounds. Images you have associated with control can be drawn in Alpha Blended form. Texts in the control can be drawn with advanced effects. All these attractions can also be personalized for different states of the controls.

Full.png

Using the Code

The project contains the following files:

  • BorderStyles.vb (Specifies the border style of the Control)
  • ImageSizes.vb (Specifies the Image Size of the Control)
  • uLabelXDesigner.vb (Specifies the class used to implement design-time services for the control)
  • uLabelX.vb (Specifies the custom Control Class)

ULabelX_Prj.png

The Control Class uLabelX Inherited from the base class Control.

VB.NET
Public Class uLabelX 
	Inherits Control 

Constructor

VB.NET
Public Sub New()
InitializeComponent()
	Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
	Me.SetStyle(ControlStyles.ResizeRedraw, True)
	Me.SetStyle(ControlStyles.Opaque, False)
	Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
		AddHandler Me.PropertyChanged, AddressOf OnPropertyChanged
		RaiseEvent PropertyChanged(False)
End Sub

Events

Event Declaration
VB.NET
Protected Event PropertyChanged(ByVal _RecreateHandle As Boolean)
Method Invoked when Property Changed
VB.NET
Private Sub OnPropertyChanged(ByVal _RecreateHandle As Boolean) 
 	If (_RecreateHandle = True) Then Me.RecreateHandle() 
	Me.Invalidate() 
End Sub 

Properties

Properties.png

VB.NET
' Gets or sets the Image of the control 
Public Property Image() As Image 
	Get 
		Return m_Image 
	End Get 
	Set(ByVal value As Image) 
		m_Image = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
VB.NET
' Gets or sets the ImageSize of the control 
Public ReadOnly Property ImageSize() As ImageSizes 
	Get 
		Return m_ImageSize 
	End Get 
End Property 
VB.NET
' Gets or sets the BorderStyle of the control 
Public Shadows Property BorderStyle() _
	As iGreen.Controls.uControls.uLabelX.Common.BorderStyles 
	Get 
		Return m_BorderStyle 
	End Get 
	Set(ByVal value As iGreen.Controls.uControls.uLabelX.Common.BorderStyles) 
		m_BorderStyle = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 

Panel2.png

VB.NET
' Gets or sets the BorderDashStyle of the control 
Public Property BorderDashStyle() As System.Drawing.Drawing2D.DashStyle 
	Get 
		Return m_BorderDashStyle 
	End Get 
	Set(ByVal value As System.Drawing.Drawing2D.DashStyle) 
		m_BorderDashStyle = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
VB.NET
' Gets or sets the BorderWidth of the control 
Public Property BorderWidth() As Single 
	Get 
		Return m_BorderWidth 
	End Get 
	Set(ByVal value As Single) 
		m_BorderWidth = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
VB.NET
' Gets or sets the BorderColor of the control 
Public Property BorderColor() As Color 
	Get 
		Return m_BorderColor 
	End Get 
	Set(ByVal value As Color)
	m_BorderColor = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 

Top.png

VB.NET
' Gets or sets the Text of the control. 
' Represents Text as a series of Unicode characters. 
Public Overrides Property Text() As String 
	Get 
		Return Replace(MyBase.Text, "ULabelX", "uLabelX") 
	End Get 
	Set(ByVal value As String) 
		MyBase.Text = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
VB.NET
' Specifies alignment of Text on the control. 
' Placement and direction of text in relation to the control border 
Public Property TextAlign() As ContentAlignment 
	Get 
		Return m_TextAlign 
	End Get 
	Set(ByVal value As ContentAlignment) 
		m_TextAlign = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 

Methods

Here we are Overriding OnPaintBackground(), otherwise the background draw task spoils the recently repainted parent control content by crushing out the OnPaintBackground() method.

VB.NET
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs) 
	' NOTHING TODO: 
End Sub 
Overriding the following to Refresh the control
VB.NET
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
	RaiseEvent PropertyChanged(True)
End Sub 
VB.NET
Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs)
	RaiseEvent PropertyChanged(True) 
End Sub
Overriding the base class OnPaint() to draw Control
VB.NET
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e) 
	e.Graphics.CompositingMode = CompositingMode.SourceOver 
	e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected 
	e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 
		Call DrawControlBorder(e.Graphics, Me.ClientRectangle) 
		Call DrawControlText(e.Graphics, Me.ClientRectangle) 
		Call DrawControlImage(e.Graphics, Me.ClientRectangle) 
End Sub 

In order to remove the unnecessary properties from the designtime property page, here I’m using the ControlDesigner Class’s PostFilterProperties.

VB.NET
Imports System.Windows.Forms.Design
Namespace Common
    Friend Class uLabelXDesigner
        Inherits ControlDesigner
        Protected Overrides Sub PostFilterProperties_
	(ByVal _Properties As System.Collections.IDictionary)
            _Properties.Remove("BackColor")
            _Properties.Remove("BackgroundImage")
            _Properties.Remove("BackgroundImageLayout")
            _Properties.Remove("RightToLeft")
            _Properties.Remove("TabStop")
            _Properties.Remove("TabIndex")
            _Properties.Remove("AutoSize")
            MyBase.PostFilterProperties(_Properties)
        End Sub
    End Class
End Namespace

Hope this can be helpful.
Happy programming !!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSmartDevice Pin
PabloDKP18-Aug-10 7:03
PabloDKP18-Aug-10 7:03 
GeneralRe: SmartDevice Pin
Koshy Panicker John18-Aug-10 7:43
Koshy Panicker John18-Aug-10 7:43 
GeneralRe: SmartDevice Pin
PabloDKP18-Aug-10 8:27
PabloDKP18-Aug-10 8:27 
GeneralRe: SmartDevice Pin
Koshy Panicker John18-Aug-10 9:16
Koshy Panicker John18-Aug-10 9:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.