home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / gspolyfm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.7 KB  |  148 lines

  1. /*
  2. Function(s) demonstrated in this program: GetPolyFillMode
  3.                       SetPolyFillMode
  4.  
  5. Description:  This program demonstrates differences in the two
  6.           Polygon filling modes, ALTERNATE and WINDING.  In ALTERNATE
  7.           mode, every other region is filled.  In WINDING, all regions
  8.           are filled.  The two shapes drawn here demonstrate the
  9.           difference.
  10.  
  11.           SetPolyFillMode is used to select the mode;
  12.  
  13.           GetPolyFillMode is used to retrieve the current fill mode and
  14.           print it on the screen while the polygon is drawn.
  15.  
  16. */
  17.  
  18. #include <windows.h>
  19.  
  20. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  21. void DrawPoly (HDC, short, short, short, short);
  22.  
  23.  
  24. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  25.     HANDLE    hInstance, hPrevInstance;
  26.     LPSTR    lpszCmdLine;
  27.     int     nCmdShow;
  28.     {
  29.     static char szAppName[]="Get/Set/PolyFillMode";
  30.     static BYTE byExtra[]={32,66,121,32,75,114,97,105,103,32,
  31.                66,114,111,99,107,115,99,104,109,105,100,116,32};
  32.     HWND    hWnd;
  33.     WNDCLASS    wndclass;
  34.     MSG     msg;
  35.  
  36.     if (!hPrevInstance)
  37.      {
  38.      wndclass.style     = CS_HREDRAW | CS_VREDRAW;
  39.      wndclass.lpfnWndProc    = WndProc;
  40.      wndclass.cbClsExtra    = 0;
  41.      wndclass.cbWndExtra    = 0;
  42.      wndclass.hInstance    = hInstance;
  43.      wndclass.hIcon     = LoadIcon(NULL, IDI_APPLICATION);
  44.      wndclass.hCursor    = LoadCursor(NULL, IDC_ARROW);
  45.      wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  46.      wndclass.lpszMenuName    = szAppName;
  47.      wndclass.lpszClassName = szAppName;
  48.  
  49.      if (!RegisterClass(&wndclass))
  50.           return FALSE;
  51.      }
  52.  
  53.     hWnd=CreateWindow(szAppName, szAppName,
  54.            WS_OVERLAPPEDWINDOW,
  55.            CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
  56.            NULL, NULL, hInstance, NULL);
  57.  
  58.     ShowWindow(hWnd, nCmdShow);
  59.     UpdateWindow(hWnd);
  60.  
  61.     while (GetMessage(&msg, NULL, 0, 0))
  62.     {
  63.     TranslateMessage(&msg);
  64.     DispatchMessage(&msg);
  65.     }
  66.     return (msg.wParam);
  67.     }
  68.  
  69. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  70.     HWND    hWnd;
  71.     unsigned    iMessage;
  72.     WORD    wParam;
  73.     LONG    lParam;
  74.     {
  75.     HDC     hDC;
  76.     HMENU    hMenu;
  77.     HPEN    hPen;
  78.     PAINTSTRUCT ps;
  79.  
  80.     switch(iMessage)
  81.     {
  82.     case WM_PAINT:
  83.         hDC=BeginPaint(hWnd, &ps);
  84.         hPen=CreatePen(PS_SOLID, 2, 0L);
  85.  
  86.         SelectObject(hDC, hPen);
  87.         SelectObject(hDC, GetStockObject(GRAY_BRUSH));
  88.  
  89.         TextOut(hDC, 10, 10, "PolyFillMode=", 13);
  90.         TextOut(hDC, 10, 190, "ALTERNATE", 9);
  91.         TextOut(hDC, 140,190, "WINDING", 7);
  92.  
  93.         DrawPoly (hDC,  40,  70, ALTERNATE, 0);
  94.         DrawPoly (hDC, 170,  70, WINDING,    0);
  95.         DrawPoly (hDC,  40, 150, ALTERNATE, 1);
  96.         DrawPoly (hDC, 170, 150, WINDING,    1);
  97.  
  98.         EndPaint(hWnd, &ps);
  99.         DeleteObject(hPen);
  100.         break;
  101.  
  102.     case WM_DESTROY:
  103.         PostQuitMessage(0);
  104.         break;
  105.  
  106.     default:
  107.         return DefWindowProc (hWnd, iMessage, wParam, lParam);
  108.     }
  109.     return (0L);
  110.     }
  111.  
  112. void DrawPoly (hDC, xOrg, yOrg, nMode, nPt)
  113.     HDC     hDC;
  114.     short    xOrg, yOrg, nMode, nPt;
  115.     {
  116.     short    xOld, yOld;
  117.     long    lView;
  118.     /* Define points in figures */
  119.     static POINT pts0[]={0,30, -30,-30, 30,0, -30,30,
  120.              0,-30, 30,30, -30,0, 30,-30,
  121.              0,30};
  122.  
  123.     static POINT pts1[]={0,-30, 25,25, -35,-10,
  124.              35,-10, -25,25, 0,-30};
  125.  
  126.     xOld=LOWORD(lView=GetViewportOrg(hDC));
  127.     yOld=HIWORD(lView);
  128.  
  129.     SetPolyFillMode(hDC, nMode);
  130.     if (GetPolyFillMode(hDC)==ALTERNATE)
  131.     TextOut(hDC, 40,20, "ALTERNATE",9);
  132.     else
  133.     TextOut(hDC, 40,20, "WINDING  ",9);
  134.  
  135.     /* Set selected origin */
  136.     SetViewportOrg(hDC, xOrg, yOrg);
  137.  
  138.     /* Draw either Polygon 1 or 2 */
  139.     if (nPt)
  140.     Polygon(hDC, pts1, sizeof(pts1)/sizeof(POINT));
  141.     else
  142.     Polygon(hDC, pts0, sizeof(pts0)/sizeof(POINT));
  143.  
  144.     /* Restore old origin */
  145.     SetViewportOrg(hDC, xOld, yOld);
  146.     return;
  147.     }
  148.