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

Last reviewed: June 21, 1995
Article ID: Q71103
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows,

  versions 1.0, 2.0, and 3.0

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.
ScaleMode = 3             ' Set the scale mode to pixels, not twips
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 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".


Additional reference words: 1.00 2.00 3.00
KBCategory: kbgraphic kbprg kbcode
KBSubcategory: APrgGrap


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.