How to make a label transparent?

Hi,
I need to know if there's a way to make a label's background transparent, I tried using the color.Transparent property on the label.backcolor, but it didn't work. In fact, i've got a picture and a label in front of it which should display a specific message. This image's color may vary so I ca'nt set a specific color to the label's background.
Is there any solution?
Thanks!
[408 byte] By [Seta] at [2008-2-16]
# 1

It is impossible to make a window transparent on netcf/windows ce, but you can make it appear as if a window was transparent.

There are 2 basic approaches for solving your problem.

1. Implement a custom control that draws text in its OnPaint method, and draws the appropriate section of the picture in its OnPaintBackground method.

2. Do not use a control for displaying your message at all. In a paint handler of the control underneath, draw the text you wish to display.

David Wrighton
.NET Compact Framework

DavidWrighton at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 2
Okay, that worked fine for me, thanks!
I used the second method which appreared to be to be the easiest solution.
If anyone finds this post and wonders how this works, here's the code :

Private Sub PctBPush_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PctBPush.Paint
Dim gr As Graphics = e.Graphics
Dim font As New System.Drawing.Font("Verdana", 8, FontStyle.Bold)
Dim brush As New SolidBrush(Color.Black)
gr.DrawString(message, font, brush, PctBPush.Left + 60, PctBPush.Top - 35)
End Sub


Seta at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 3

You can make label Really Transparent by

  • Returning &H20 from CreateParams
Code Snippet
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
  • Setting OptimizedDoubleBuffer to False

Code Snippet
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub

  • Overriding OnDrawBackground

Code Snippet
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
If MyBase.BackgroundImage Is Nothing Then
pevent.Graphics.DrawRectangle(New Pen(Me.BackColor, 1), pevent.ClipRectangle)
Else
MyBase.OnPaintBackground(pevent)
End If
End Sub

More on http://dzonny.cz/Software/Blog/tabid/93/EntryID/2/Default.aspx, working example on http://codeplex.com/DTools

?onny at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 4

The sample provided by ?onny will not work correctly on the Compact Framework due to missing functionality in both the Compact Framework and the underlying Windows CE operating system.

For instance the SetStyle method is not available and the WS_EX_TRANSPARENT (&H20) window style is not supported (as mentioned by David Wrighton).

You may also like to have a look at the following thread for some alternative (more designer IDE friendly) techniques for implementing this http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1557544&SiteID=1. In particular Alex yakhnin's technique listed in his blog here http://blog.opennetcf.org/ayakhnin/PermaLink,guid,34221459-8db8-41ef-91c7-5514eade8fca.aspx.

Hope it helps,

Christopher Fairbairn

ChristopherFairbairn at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 5
I'm sorry I'haven't notice that this is Compact Framework forum :-(
?onny at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...