Polygon is the sixth function for drawing a bordered and filled figure. The function call is similar to the PolyLine function:
Polygon (hdc, lpPoints, nCount) ;
The lpPoints parameter is a far pointer to an array of POINT structures (in logical coordinates), and nCount is the number of points. If the last point in this array is different from the first point, Windows adds another line that connects the last point with the first point. (This does not happen with the PolyLine function.)
Windows fills this bounded area with the current brush in one of two ways, depending on the current polygon filling mode defined in the device context. By default, the polygon filling mode is ALTERNATE, which means that Windows fills in only those interiors accessible from the outside of the polygon by crossing an odd number of lines (1, 3, 5, and so forth). The other interiors are not filled. You can also set the polygon filling mode to WINDING, in which case Windows fills in all the interior areas. The two polygon filling modes are most simply demonstrated with a five-pointed star. In Figure 12-12 on the following page, the star on the left was drawn with the ALTERNATE mode, and the star on the right was drawn with the WINDING mode. Both figures were drawn with an array of points defined like this:
static POINT pt [] =
{ -59, -81, 0, 100, 59, -81, -95, 31, 95, 31 } ;
The five points of the star were manually calculated from trigonometric tables. The WM_PAINT logic looks like this:
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
hPen = CreatePen (PS_SOLID, 3, 0L) ;
SelectObject (hdc, hPen) ;
SelectObject (hdc, GetStockObject (LTGRAY_BRUSH)) ;
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExt (hdc, 440, -220) ;
SetViewportExt (hdc, xClient, yClient) ;
SetWindowOrg (hdc, -110, 110) ;
SetPolyFillMode (hdc, ALTERNATE) ;
Polygon (hdc, pt, sizeof (pt) / sizeof (POINT)) ;
SetWindowOrg (hdc, -330, 110) ;
SetPolyFillMode (hdc, WINDING) ;
Polygon (hdc, pt, sizeof (pt) / sizeof (POINT)) ;
EndPaint (hwnd, &ps) ;
DeleteObject (hPen) ;
break ;
The PolyPolygon function draws multiple polygons.