Yes it is possible. Add a new user control file to your project and
copy/replace the following code. You now can create label controls of type
'TransparentLabel' which will be transparent:
Imports System.ComponentModel
Public Class TransparentLabel
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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()
'
'TransparentLabel
'
Me.BackColor = System.Drawing.Color.DeepPink
Me.Name = "TransparentLabel"
Me.Size = New System.Drawing.Size(256, 46)
End Sub
#End Region
Private Txt As String
Private AutoSze As Boolean = True
<Bindable(True), Category("Appearance"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
_
Browsable(True)> _
Public Overrides Property Text() As String
Get
Return Txt
End Get
Set(ByVal Value As String)
Txt = Value
Me.Refresh()
End Set
End Property
<Category("Behavior"), DefaultValue(True)> Public Property AutoSize() As
Boolean
Get
Return AutoSze
End Get
Set(ByVal Value As Boolean)
AutoSze = Value
Me.Refresh()
End Set
End Property
Private Sub TransparentLabel_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Try
If AutoSze = True Then
Dim Siz As Drawing.SizeF = e.Graphics.MeasureString(Txt,
Font)
Me.Width = Siz.Width + 1
Me.Height = Siz.Height + 1
End If
Dim B As New Bitmap(Me.Width, Me.Height)
Dim G As Graphics = Graphics.FromImage(B)
G.FillRectangle(New SolidBrush(Color.DeepPink), New Rectangle(0,
0, Me.Width, Me.Height))
G.DrawString(Txt, Font, New SolidBrush(Me.ForeColor), 0, 0)
Dim Pth As New Drawing2D.GraphicsPath()
Dim X, Y As Short
For X = 0 To Me.Width - 1
For Y = 0 To Me.Height - 1
If B.GetPixel(X, Y).ToArgb.ToString =
Color.DeepPink.ToArgb.ToString Then
Pth.AddRectangle(New Rectangle(X, Y, 1, 1))
End If
Next
Next
G.FillRectangle(New SolidBrush(Me.ForeColor), New Rectangle(0,
0, Me.Width, Me.Height))
e.Graphics.DrawImage(B, 0, 0)
Dim Rgn As New Region(New Rectangle(0, 0, Me.Width, Me.Height))
Rgn.Exclude(Pth)
Me.Region = Rgn
B.Dispose()
G.Dispose()
Catch
End Try
End Sub
Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs)
Me.Refresh()
End Sub
Private Sub TransparentLabel_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
setstyle(ControlStyles.AllPaintingInWmPaint, True)
setstyle(ControlStyles.DoubleBuffer, True)
setstyle(ControlStyles.UserPaint, True)
End Sub
End Class