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
 
 
 
 
 
 
TitleTile a polygon with a picture
Keywordspicture, tile, polygon, region
CategoriesGraphics, API
 

At design time, a hidden PictureBox is initialized to contain the picture.

When the program starts, the Form_Load event handler uses some trigonometry to calculate the points needed to draw an eight-pointed star and stores them in the pts array of POINTAPI structures. It uses the CreatePolygonRgn API function to make a region defined by those points. It then calls SetWindowRgn to confine the PictureBox to that region. Any pieces of the loaded picture that fall outside the region are clipped off automatically.

The program then loops over the area in the PictureBox using PaintPicture to copy the hidden picture onto it.

 
Private Sub Form_Load()
Const PI = 3.14159265
Const NUM_POINTS = 8
Const NUM_PTS = 2 * NUM_POINTS
Const TXT = "Star "

Dim pts(1 To NUM_PTS) As POINTAPI
Dim i As Integer
Dim cx As Single
Dim cy As Single
Dim theta As Single
Dim dtheta As Single
Dim rx1 As Single
Dim rx2 As Single
Dim ry1 As Single
Dim ry2 As Single
Dim hRgn As Long
Dim clr As Single
Dim dclr As Single
Dim offset As Single
Dim X As Single
Dim Y As Single

    ' API functions work in pixels.
    Me.ScaleMode = vbPixels
    Me.AutoRedraw = True
    Me.BorderStyle = vbBSNone

    ' Set the points' coordinates.
    cx = Me.ScaleWidth / 2
    cy = Me.ScaleHeight / 2
    rx1 = Me.ScaleWidth * 0.49
    rx2 = Me.ScaleWidth * 0.25
    ry1 = Me.ScaleHeight * 0.49
    ry2 = Me.ScaleHeight * 0.25
    dtheta = 2 * PI / NUM_PTS
    theta = 0
    i = 1
    Do While i < NUM_PTS
        pts(i).X = cx + rx1 * Cos(theta)
        pts(i).Y = cy + ry1 * Sin(theta)
        theta = theta + dtheta
        i = i + 1

        pts(i).X = cx + rx2 * Cos(theta)
        pts(i).Y = cy + ry2 * Sin(theta)
        theta = theta + dtheta
        i = i + 1
    Loop

    ' Confine the form to this polygon.
    hRgn = CreatePolygonRgn(pts(1), NUM_PTS, WINDING)
    SetWindowRgn Me.hWnd, hRgn, True

    ' Tile the form with the picture.
    picTile.ScaleMode = vbPixels
    picTile.Visible = False

    Y = 0
    Do While Y < ScaleHeight
        X = 0
        Do While X < ScaleWidth
            PaintPicture picTile.Picture, X, Y
            X = X + picTile.ScaleWidth
        Loop
        Y = Y + picTile.ScaleHeight
    Loop
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated