• Welcome to PlanetSquires Forums.
 

CWindow: Petzold's Alternate and Winding Fill Modes

Started by José Roca, September 11, 2015, 01:30:24 AM

Previous topic - Next topic

José Roca


' ########################################################################################
' Microsoft Windows
' File: CW_Petzold_AltWind.fbtpl - Template
' This program is a translation/adaptation of the ALTWIND.C-Alternate and Winding Fill
' Modes Program © Charles Petzold, 1998, described and analysed in Chapter 5 of the book
' Programming Windows, 5th Edition.
' Displays the figure twice, once using the ALTERNATE filling mode and then using WINDING.
' Compiler: FreeBasic 32 & 64 bit
' Copyright (c) 2015 Jose Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#define unicode
#INCLUDE ONCE "windows.bi"
#INCLUDE ONCE "Afx/CWindow.inc"
#INCLUDE ONCE "Afx/AfxWin.inc"

USING Afx.CWindowClass

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG


   END WinMain(GetModuleHandleW(""), NULL, COMMAND(), SW_NORMAL)

' ========================================================================================
' Window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hWnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

   DIM hdc AS HDC
   DIM i AS LONG
   DIM ps AS PAINTSTRUCT
   DIM apt(9) AS POINT
   STATIC aptFigure(9) AS POINT
   STATIC cxClient AS LONG
   STATIC cyClient AS LONG

   FUNCTION = 0

   SELECT CASE AS CONST uMsg

      CASE WM_CREATE
         aptFigure(0).x = 10 : aptFigure(0).y = 70
         aptFigure(1).x = 50 : aptFigure(1).y = 70
         aptFigure(2).x = 50 : aptFigure(2).y = 10
         aptFigure(3).x = 90 : aptFigure(3).y = 10
         aptFigure(4).x = 90 : aptFigure(4).y = 50
         aptFigure(5).x = 30 : aptFigure(5).y = 50
         aptFigure(6).x = 30 : aptFigure(6).y = 90
         aptFigure(7).x = 70 : aptFigure(7).y = 90
         aptFigure(8).x = 70 : aptFigure(8).y = 30
         aptFigure(9).x = 10 : aptFigure(9).y = 30
         EXIT FUNCTION

      CASE WM_SIZE
         cxClient = LOWORD(lParam)
         cyClient = HIWORD(lParam)
         EXIT FUNCTION

      CASE WM_PAINT
         hdc = BeginPaint(hwnd, @ps)
         SelectObject hdc, GetStockObject(GRAY_BRUSH)
         FOR i = 0 TO 9
            apt(i).x = cxClient * aptFigure(i).x / 200
            apt(i).y = cyClient * aptFigure(i).y / 100
         NEXT
         SetPolyFillMode hdc, ALTERNATE
         Polygon hdc, @apt(0), 10
         FOR i = 0 TO 9
            apt(i).x = apt(i).x + cxClient / 2
         NEXT
         SetPolyFillMode hdc, WINDING
         Polygon hdc, @apt(0), 10
         EndPaint hwnd, @ps
         EXIT FUNCTION

      CASE WM_COMMAND
         SELECT CASE LOWORD(wParam)
            CASE IDCANCEL
               IF HIWORD(wParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

    CASE WM_DESTROY
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI awaree
   AfxSetProcessDPIAware

   DIM pWindow AS CWindow
   pWindow.Create(NULL, "Alternate and Winding Fill Modes", @WndProc, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT)
   pWindow.Center

   FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================