What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDraw text filled with text
Keywordsfilled text, outline text, text, CreateFont, SetWindowRgn, region
Use the CreateFont API function to make the desired font. Be sure to use a TrueType font. The CustomFont function make this a little easier.
' 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
After you create the font, install it in the Form or PictureBox where you want to draw with the SelectObject API function.

Call BeginPath, write the text, and call EndPath to convert the text into a graphic path. Then call SetWindowRgn to restrict the Form/PictureBox to the region. Now anything you draw will be clipped to fit the region. Draw the small text inside the larger text.

Use SelectObject to restore the original font and DeleteObject to delete the new font, freeing up its graphic resources. This is impoertant. If you don't do this, the system may run out of resources.

Private Sub ShapePicture()
Const TEXT2 = "rose tulip carnation daffodil peony daisy " & _
    "dandelion snapdragon pansy "

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 = 1

    ' 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

    ' Draw text in the PictureBox.
    With Picture1.Font
        .Name = "Times New Roman"
        .Size = 8
        .Bold = False
    End With
    Picture1.CurrentY = 0
    Do While Picture1.CurrentY <= Picture1.ScaleHeight
        Picture1.CurrentX = -Picture1.CurrentY
        Do While Picture1.CurrentX <= Picture1.ScaleWidth
            Picture1.Print TEXT2;
        Picture1.CurrentY = Picture1.CurrentY + _
End Sub
For more information on graphics programming in Visual Basic (including advanced font use and regions), see my book Visual Basic Graphics Programming.

Click here to compare this code to the version used for VB .NET.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.