none
How to make a label transparent?

    Question

  • 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!
    Monday, July 18, 2005 10:16 AM

Answers

  • 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

    Wednesday, July 20, 2005 10:51 PM

All replies

  • 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

    Wednesday, July 20, 2005 10:51 PM
  • 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

     

    Thursday, July 21, 2005 7:57 AM
  • 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

     

    Sunday, May 13, 2007 6:03 PM
  • 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

    Sunday, May 13, 2007 10:39 PM
  • I'm sorry I'haven't notice that this is Compact Framework forum :-(
    Monday, May 14, 2007 11:09 AM
  • نحمده ونصلي على رسوله الكريم

    label1.Parent = pictureBox1;

    • Proposed as answer by Jawad Khan Friday, May 11, 2012 6:13 AM
    Thursday, April 19, 2012 4:36 PM