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
 
 
 
 
 
TitleFill a polygon with a color gradient
Keywordspolygon, gradient text, SetWindowRgn, region
CategoriesGraphics
 

This program uses sines and cosines to fill a POINTAPI array with the coordinates of the points that make up a star shape. It calls the CreatePolygonRgn API function to make a region defined by the points. Then it calls SetWindowRgn to restrict a PictureBox to the region.

Finally, the program draws a series of colored lines through the PictureBox. Each line is a slightly different shade of green.

 
' Draw a gradient-filled polygon.
Private Sub DrawPolygon()
Const PI = 3.14159265
Const NUM_POINTS = 8
Const NUM_PTS = 2 * NUM_POINTS

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

    ' API functions work in pixels.
    Picture1.Move 0, 0, ScaleWidth, ScaleHeight
    Picture1.ScaleMode = vbPixels
    Picture1.AutoRedraw = True

    If Picture1.ScaleHeight < 1 Then Exit Sub
    If Picture1.ScaleWidth < 1 Then Exit Sub

    ' Set the points' coordinates.
    cx = Picture1.ScaleWidth / 2
    cy = Picture1.ScaleHeight / 2
    rx1 = Picture1.ScaleWidth * 0.49
    rx2 = Picture1.ScaleWidth * 0.25
    ry1 = Picture1.ScaleHeight * 0.49
    ry2 = Picture1.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 PictureBox to this polygon.
    hRgn = CreatePolygonRgn(pts(1), NUM_PTS, WINDING)
    SetWindowRgn Picture1.hWnd, hRgn, True

    ' Draw a color gradient through the picture.
    clr = 256
    dclr = -255 / Picture1.ScaleHeight
    For i = 0 To Picture1.ScaleHeight
        Picture1.Line (0, i)-(Picture1.ScaleWidth, i), _
            RGB(0, clr, 0)
        clr = clr + dclr
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated