How to Use FillPolygonRgn API to Fill Shape in Visual Basic

ID Number: Q81470

1.00

WINDOWS

Summary:

Visual Basic does not provide a method of filling graphics shapes with

color other than the Line statement with the BF (Box Fill) parameter.

To create a polygon region on a form or picture and fill it with a

color, you can use the CreatePolygonRgn and FillRgn Windows API calls

to draw and fill areas of the screen with color.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

To draw a polygon on a form or picture control, you can use the

Polygon API call; this will draw the edge of the polygon. You can then

use CreatePolygonRgn to create an area that you can paint and use

FillRgn to fill it with a color. Using these Windows API calls allows

you to pick the points, the number of points, and to choose the color

or brush to fill with.

The API calls used in this example should be declared in your Global

module or in the general Declarations section of your form. They are

as follows:

API Call Description

-------- -----------

CreatePolygonRgn Creates a polygonal region

GetStockObject Retrieves a handle to one of the predefined stock

pens, brushes, or fonts

FillRgn Fills the region specified by the hRgn parameter

with the brush specified by the hBrush parameter

Polygon Draws a polygon consisting of two or more points

connected by lines

Code Example

------------

The following code example shows how to create a black triangle on a

form. To change the program to create other shapes, add points to the

array.

1. From the Visual Basic environment (VB.EXE), add the following code

to the Global module or to the general Declaration section:

Type Coord ' This is the type structure for the x and

x As Integer ' y coordinates for the polygonal region

y As Integer

End Type

Declare Function CreatePolygonRgn Lib "gdi"

(lpPoints As Any, ByVal nCount As Integer,

ByVal nPolyFillMode As Integer) As Integer

Declare Function Polygon Lib "gdi"

(ByVal hDC As Integer, lpPoints As Any,

ByVal nCount As Integer) As Integer

Declare Function FillRgn Lib "gdi"

(ByVal hDC As Integer, ByVal hRgn As Integer,

ByVal hBrush As Integer) As Integer

Declare Function GetStockObject Lib "gdi"

(ByVal nIndex As Integer) As Integer

Global Const ALTERNATE = 1 ' ALTERNATE and WINDING are

Global Const WINDING = 2 ' constants for FillMode

Global Const BLACKBRUSH = 4' constant for brush type

2. Add the following code to the Form1.Click event:

Sub Form_Click ()

' dimension coordinate array

ReDim poly(1 To 3) As Coord

' Number of vertices in polygon

NumCoords% = 3

' set scalemode to pixels to set up points of triangle

form1.scalemode = 3

' assign values to points

poly(1).x = form1.scalewidth / 2

poly(1).y = form1.scaleheight / 2

poly(2).x = form1.scalewidth / 4

poly(2).y = 3 * form1.scaleheight / 4

poly(3).x = 3 * form1.scalewidth / 4

poly(3).y = 3 * form1.scaleheight / 4

' sets background color to red for contrast

form1.backcolor = &HFF

' Polygon function creates unfilled polygon on screen.

' remark FillRgn statement to see results.

bool% = Polygon(form1.hdc, poly(1), NumCoords%)

' gets stock black brush

hbrush% = GetStockObject(BLACKBRUSH)

' creates region to fill with color

hrgn% = CreatePolygonRgn(poly(1), NumCoords%, ALTERNATE)

' if the creation of the region was successful then color

If hrgn% Then bool% = FillRgn(form1.hdc, hrgn%, hbrush%)

' Print out some info

Print "FillRgn Return : ";bool%

Print "HRgn : "; hrgn%

Print "Hbrush : "; hbrush%

End Sub

3. Run the program.

You should initially see a blank form. Click the form; a red

background with a black triangle on it should be displayed. You can

try different numbers of vertices by adding elements to the poly array

and updating NumCoords. Different colors and brushes can be

substituted as desired.

Note: If you try to fill a region with coordinates beyond the visible

form, the CreatePolygonRgn function call will return a zero, meaning it

was unsuccessful. The FillRgn will not work if the CreatePolygonRgn

function was unsuccessful. All you will see is the outline created by

the Polygon function. You should make certain that the vertices are

all within the viewable form.

Additional reference words: 1.00