How to Flood Fill (Paint) in VB using ExtFloodFill Windows API

ID Number: Q71103

1.00

WINDOWS

Summary:

You can fill an area on a window in Visual Basic through a Windows API

function call. Depending on the type of fill to be performed, you can

use the ExtFloodFill function to achieve the desired effect. This

feature is similar to the paint feature found in painting programs.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

The Windows API ExtFloodFill function call fills an area of the

display surface with the current brush, as shown in the example below.

Code Example

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

From the VB.EXE Code menu, choose View Code, and enter the following

code (on just one line) for Form1 (using [general] from the Object box

and [declarations] from the Procedure box):

Declare Function ExtFloodFill Lib "GDI" (ByVal hdc%, ByVal i%,

ByVal i%, ByVal w&, ByVal i%) As Integer

To demonstrate several fill examples, create a picture box called

Picture1. Set the following properties:

AutoSize = TRUE ' Scale picture to size of imported picture.

FillColor = &HFF00FF ' This will be the selected fill color.

FillStyle = Solid ' Necessary to create a fill pattern.

Picture = Chess.bmp ' This should be in your Windows directory.

Create a push button in a location that will not be overlapped by

Picture1. Within the Click event, create the following code:

Sub Command1_Click ()

' Make sure that the FillStyle is not transparent.

' crColor& specifies the color for the boundary.

Const FLOODFILLBORDER = 0 ' Fill until crColor& color encountered.

Const FLOODFILLSURFACE = 1 ' Fill surface until crColor& color not

' encountered.

X% = 1

Y% = 1

crColor& = RGB(0, 0, 0)

wFillType% = FLOODFILLSURFACE

Suc% = ExtFloodFill(picture1.hDC, X%, Y%, crColor&, wFillType%)

End Sub

When you click on the push button, the black background will change to

the FillColor. The fill area is defined by the color specified by

crColor&. Filling continues outward from (X%,Y%) as long as the color

is encountered.

Now change the related code to represent the following:

crColor& = RGB(255, 0, 0) 'Color to look for.

wFillType% = FLOODFILLBORDER

Suc% = ExtFloodFill(picture1.hDC, X%, Y%, crColor&, wFillType%)

Executing the push button will now fill the area until crColor& is

encountered. In the first example, the fill was performed while the

color was encountered; in the second example, the fill was performed

while the color was NOT encountered. In the last example, everything

is changed except the "floating pawn".

Reference(s):

"Programming Windows: the Microsoft Guide to Writing Applications for

Windows 3," by Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1,"

version 3.0

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit