What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a "heavyweight" label that can sit on top of a shockwave animation or other controls
Keywordslabel, windowless control, SetWindowRgn, region, lightweight, heavyweight
If you try to put a Label control above a shockwave animation and certain other kinds of controls, the Label either doesn't appear or is hidden behind the control. The reason is the Label control isn't really a control in the sense that other controls are. The Label and Image controls are "windowless" or "lightweight" controls. They don't have windows of their own. Instead they are drawn on their parent window. For example, a Label placed on a form is drawn on the form's window. A shockwave animation sits in its own window. It is either on top of the Label or, if the Label ever gets drawn, the animation draws over it.

To solve this problem, place a PictureBox above the animation. Then use SetWindowRgn to restrict the PictureBox to the outline of the text you want to display. Set the PictureBox's BackColor property to give the "label" a color.

' Shape the PictureBox.
Private Sub ShapePicture()

Dim new_font As Long
Dim old_font As Long
Dim hRgn As Long
Dim offset As Single

    ' Prepare the PictureBox.
    ScaleMode = vbPixels
    Picture1.AutoRedraw = True
    Picture1.ScaleMode = vbPixels
    Picture1.BorderStyle = vbBSNone

    ' Make a big font.
    new_font = CustomFont(250, 65, 0, 0, _
        FW_BOLD, False, False, False, _
        "Times New Roman")
    old_font = SelectObject(Picture1.hdc, new_font)

    ' Make the region.
    SelectObject Picture1.hdc, new_font
    BeginPath Picture1.hdc
    Picture1.CurrentX = (ScaleWidth - _
        Picture1.TextWidth(TEXT1)) / 2
    Picture1.CurrentY = -40
    Picture1.Print TEXT1
    EndPath Picture1.hdc
    hRgn = PathToRegion(Picture1.hdc)

    ' Constrain the PictureBox to the region.
    SetWindowRgn Picture1.hWnd, hRgn, False

    ' Restore the original font.
    SelectObject hdc, old_font

    ' Free font resources (important!)
    DeleteObject new_font

    ' Set the PictureBox's background color.
    Picture1.BackColor = vbBlue
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.