Quantcast
Channel: SourceHints » classes
Viewing all articles
Browse latest Browse all 5

Creating a real transparent label in vb.net

$
0
0

Post to Twitter

Creating a true transparent label.

  • Requirements: Visual Studio 2010
  • Programming Level: Advance
  • Language: Visual Basic.net

Scenario:

I have been looking for quite some time a control that can give me a true transparent label in which we can see the background color or object behind this control just like the old VB6 language, by just setting up the BackStyle Property of VB6 Label Control. As Shown Below:

Microsoft Visual Basic 6 Label Control

Microsoft Visual Basic 6 Label Control

But unfortunately in visual basic.net it is not possible as visual basic.net treated this in different way, it only copy the background color of the form and really making it a true transparent. Well in this article I am going to show you how i was able to create a true transparent label in visual basic.net.  You need to create a new project in Visual Studio 2010 and add another project in your solution and select Class Library as project Type.  Then add a user control in this Class Library Project. The Code is given below:

You shall need to add These References in your Class Project:

System.Drawing
System.Windows.Forms
System.ComponentModel

The Source Code


Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Public Class TransparentLabel
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.Opaque, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.components = New System.ComponentModel.Container()
        RF = New RectangleF(0, 0, MyBase.Width, MyBase.Height)
        LabelForeColorBrush = New SolidBrush(MyBase.ForeColor)
    End Sub
#Region "Private"
    Private sFormat As StringFormat
    Private RF As RectangleF
    Dim LabelForeColorBrush As SolidBrush = Nothing
#End Region
#Region "Subroutines"
    Private Sub UpdateText()
        Try
            sFormat = New StringFormat
            Dim x As Integer = 0
            Dim y As Integer = 0
            With sFormat
                Select Case TextAlignment
                    Case ContentAlignment.BottomCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.BottomLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.BottomRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.MiddleLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.MiddleCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.MiddleRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.TopCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Near
                    Case ContentAlignment.TopLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Near
                    Case ContentAlignment.TopRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Near
                End Select
                .FormatFlags = StringDirection
            End With
            ResizeControl()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub ResizeControl()
        RF.Size = New Size(MyBase.Size)
        Invalidate()
    End Sub
#End Region
#Region "Properties"
    Dim _StringDirection As StringFormatFlags = StringFormatFlags.NoClip
    <Description("The Direction of the Text."), DefaultValue(StringFormatFlags.NoClip)>
    Public Property StringDirection As StringFormatFlags
        Get
            Return _StringDirection
        End Get
        Set(ByVal value As StringFormatFlags)
            _StringDirection = value
            UpdateText
        End Set
    End Property

    Private _TextAlignment As System.Drawing.ContentAlignment = ContentAlignment.MiddleCenter
    <Description("The Text Alignment that will appear on this control."), DefaultValue(ContentAlignment.MiddleCenter)>
    Public Property TextAlignment() As System.Drawing.ContentAlignment
        Get
            Return _TextAlignment
        End Get
        Set(ByVal value As System.Drawing.ContentAlignment)
            _TextAlignment = value
            UpdateText()
        End Set
    End Property

#End Region
#Region "Overrides"
    Public Overrides Property ForeColor As System.Drawing.Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.ForeColor = value
            LabelForeColorBrush = New SolidBrush(value)
        End Set
    End Property
    ''' <summary>
    ''' The text to be displayed in supports with real transparency.
    ''' </summary> 
    ''' <remarks></remarks>
    Dim _Labeltext As String = "TransparentLabel"
    <Description("The text to be displayed in supports with real transparency."), Category("Text"), DefaultValue("TransparentLabel")>
    Public Property LabelText As String
        Get
            Return _Labeltext
        End Get
        Set(ByVal value As String)
            _Labeltext = value
            Invalidate()
        End Set
    End Property

    <Browsable(False), EditorBrowsable(False)>
    Public Overrides Property BackColor As System.Drawing.Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
        End Set
    End Property
    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 OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Try
            MyBase.OnPaint(e)
            ' draw the text on the control
            e.Graphics.DrawString(LabelText, MyBase.Font, LabelForeColorBrush, RF, sFormat)
            ' MyBase.OnPaint(e)
        Catch ex As Exception

        End Try

    End Sub
#End Region
    Private Sub TransparentLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        ResizeControl()
    End Sub
End Class

The Screenshot is given below for the transparent control:

The true transparent label control in vb.net

The true transparent label control in vb.net

The Full Source Code Project Download in VB.NET and in C#.NET

You can download the full sourcecode Here in ZIP Format but please like our Facebook Fan Page first.
Or for c# enthusiast you can try my C# equivalent TransparentLabel in C Sharp Here.

Please use the password below if asked for a password during extraction:

Password: ilikesourcehints

Post to Twitter

Keywords: , , , , , , , , ,

Other reading this article are also reading these:


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images