Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw text filled with random colored circles in Visual Basic 6
DescriptionThis example shows how to draw text filled with random colored circles in Visual Basic 6.
Keywordsrandom, random circles, fonts, circle filled text, VB 6, Visual Basic 6
CategoriesGraphics, Graphics, Graphics
 

The example Draw text filled with lines explains how to use a clipping region to restrict drawing to the area within some text. This example uses a similar technique but it fills the text with random circles instead of lines.

The program uses the following code to draw its text.

 
Private Sub ShapePicture()
Const TEXT1 = "VB Helper"

Dim new_font As Long
Dim old_font As Long
Dim hRgn As Long
Dim i As Integer
Dim clr As OLE_COLOR
Dim X As Single
Dim Y As Single
Dim radius As Single

    ' 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.
    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 random circles on the PictureBox.
    Const min_radius As Integer = 10
    Const max_radius As Integer = 100
    Picture1.FillStyle = vbFSSolid
    Randomize
    For i = 1 To 200
        clr = QBColor(Int(Rnd * 16))
        X = Int(Rnd * Picture1.ScaleWidth)
        Y = Int(Rnd * Picture1.ScaleHeight)
        radius = Int(min_radius + Rnd * (max_radius - _
            min_radius))
        Picture1.FillColor = clr
        Picture1.Circle (X, Y), radius, clr
        Debug.Print X, Y, radius, clr '@
    Next i
End Sub
 
The code first sets some properties on the PictureBox. It then creates a large font and selects it for drawing with the API. Next the code creates a path and draws the text into it. It converts the path into a region and uses SetWindowRgn to restrict drawing on the PictureBox to the region so any drawing appears inside the text's area. Finally the code fills the random circles on the PictureBox.
 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated