By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,744 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

clear backstyle for lable control?

P: n/a
JD
Is it possible to have a clear backstyle for a lable
control? I want to write some text on a lable and I want
the background of the label to match the color of the
control I am placing the label on where the color of that
control will be changing will be changing. Is this
doable? If not, is there a control I can do this with? to
have a clear/see through backstyle?

Thanks,
JD
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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(DesignerSerializat ionVisibility.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


Nov 21 '05 #2

P: n/a
JD
Thanks. Will I be able to write text to this label? Do
I have to add a property to the class for this?
-----Original Message-----
Yes it is possible. Add a new user control file to your project andcopy/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 SubInitializeComponent()
'
'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() AsBoolean
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 AsSystem.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 eAs System.EventArgs) Handles MyBase.Load
setstyle(ControlStyles.AllPaintingInWmPaint, True) setstyle(ControlStyles.DoubleBuffer, True)
setstyle(ControlStyles.UserPaint, True)
End Sub
End Class


.

Nov 21 '05 #3

P: n/a
JD,

Maybe this is easier to understand.

\\\
Panel1.BackColor = Color.Yellow
Label1.BackColor = Color.Transparent
Label1.ForeColor = Color.Red
Label1.Text = "Hello JD"
////

I hope this helps,

Cor
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.