Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleDraw text filled with a picture
Keywordstext, region, path, picture
CategoriesGraphics, API
 

At design time, the PictureBox's Picture property is set to display a picture.

When the program starts, subroutine DrawFilledText calls subroutine CustomFont to make a large custom font. It uses SelectObject to select the custom font into the PictureBox. It calls the BeginPath API function, draws some text, and calls EndPath. This creates the text as a path (series of lines outlining the letters).

Next the routine calls the PathToRegion API function to convert the path into a region. It uses SetWindowRgn to constrain the PictureBox to the resulting region. Now anything drawn outside this region, including pieces of the loaded picture outside the region, is clipped off.

 
' Draw the picture filled text.
Private Sub DrawText()
Const TEXT1 = "FLOWERS"

Dim new_font As Long
Dim old_font As Long
Dim hRgn As Long

    ' Prepare the PictureBox.
    ScaleMode = vbPixels
    Picture1.AutoRedraw = True
    Picture1.ScaleMode = vbPixels
    Picture1.BorderStyle = vbBSNone
    Picture1.BackColor = vbBlue
    Picture1.ForeColor = vbBlack
    Picture1.DrawWidth = 5

    ' 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
End Sub
 
The CustomFont function calls API function CreateFont to make a custom font. You can use this routine to make fonts that are tall and thin, short and wide, and rotated at an angle.
 
' Make a customized font and return its handle.
Private Function CustomFont(ByVal hgt As Long, ByVal wid As _
    Long, ByVal escapement As Long, ByVal orientation As _
    Long, ByVal wgt As Long, ByVal is_italic As Long, ByVal _
    is_underscored As Long, ByVal is_striken_out As Long, _
    ByVal face As String) As Long
Const CLIP_LH_ANGLES = 16   ' Needed for tilted fonts.

    CustomFont = CreateFont( _
        hgt, wid, escapement, orientation, wgt, _
        is_italic, is_underscored, is_striken_out, _
        0, 0, CLIP_LH_ANGLES, 0, 0, face)
End Function
 
For more information on graphics programming in Visual Basic (including advanced font use and regions), see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated