Transparent labels

mundano

Member
Joined
Oct 5, 2006
Messages
11
Programming Experience
3-5
Hi everyone,

I would like to develop a transparent label so that it does not depend on its parent. For example, imagine you have a form with a picturebox and a label. If you set the background property of the label to 'transparent', you will see the form below the text of the label.

So far, it works fine. The problem is that, if you move the label over the picturebox, you will still see the form below the label and not the picturebox. I will need the opposite: to see the picturebox below the label and, eventually, to see both the form and the picturebox (when the label is over both of them).

I know it is somewhat tricky, so I would appreciate any hint.

Thank you beforehand.
 
I think that accomplishing this could be kind of difficult. The label will only know that the form is under it because it is only added to the components of the form. The label will not know if it is on top of another control (unless you put it on another control that has a controls collection because it would then be added to that).
 
VB.NET:
Imports System.Windows.Forms

Public Class cTransparentLabel
    Inherits Control

    Protected Overrides ReadOnly Property CreateParams() As 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)
        e.Graphics.DrawString(Me.Text, New Font("Arial", 30, FontStyle.Regular), New SolidBrush(Color.Black), 0, 0)
    End Sub

    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaintBackground(pevent)
    End Sub

End Class

and

VB.NET:
Public Class Form1

    Public TransparentLabel As cTransparentLabel = New cTransparentLabel

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        TransparentLabel.Parent = Me
        TransparentLabel.Location = New System.Drawing.Point(221, 151)
        TransparentLabel.Size = New System.Drawing.Size(100, 50)
        TransparentLabel.Text = "TST"
        TransparentLabel.BringToFront()

    End Sub
End Class

You'll need to code the font as necessary - I didnt want to do ALL the work for you :D
 
Thanks for your replies

Hi,

Thanks a lot for your reply, it has been very helpful. I have developed a label control that moves around a form and you can see trough no matter what is beneath.

As the text of the label can be filled in dinamically, i had to do some workaround to calculate its size to display the text. Finally, there is still some bug when moving around the label. The background is not refreshed until you drop it and i had to do a trick (not very elegant, though) by setting the visible property to false and then to true so that the background is displayed correctly.

If someone is interested in the code, I will be glad to post it.

Thank you very much.
 
I dont know if this will work, but you could try handling the OnLocationChanged event and repainting the control.
 
I'd be interested in seeing the code for this and playing with it a little. As InertiaM said, you can probably just handle the OnLocationChanged and invalidate the control in there causing a repaint.
 
Hi,

Been a little busy these days so i couldn't post earlier. The code is basically what InertiaM wrote with some events to handle the movement of the mouse, so i will post this event handling.

VB.NET:
    Private bmouseDown As Boolean = False 
    Private dragPoint As Point

 
    Private Sub ClaseTexto_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

            'Save the starting point of the drag&drop operation
            dragPoint.X = e.X
            dragPoint.Y = e.Y
	    bmouseDown = True

End Sub

    Private Sub ClaseTexto_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            
        If bmouseDown Then
                Me.DoDragDrop(Me, DragDropEffects.Move)
                'Here is the trick so that the label is refreshed
                Me.Visible = False
                Me.Visible = True
        End If
    End Sub

    Private Sub ClaseTexto_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
            
            bmouseDown = False
    End Sub

So more or less there it is. Since it is extracted from my code, i have not tried it, so there can be some errors. Remember to activate the AllowDrop property.

Thanks for your help.
 
Back
Top