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

  1. /*
  2.  *  Function (s) demonstrated in this program: GetClipbox
  3.  *  Description: This function retrieves the dimensions of the tightest 
  4.  *               bounding rectangle around the current clipping boundary.  
  5.  *               The dimensions are copied to the buffer pointed to by the
  6.  *               last parameter.
  7.  */
  8.  
  9. #include <windows.h>
  10. #include <stdio.h>
  11.  
  12. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  13.  
  14. HDC hDC;
  15.  
  16. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  17. HANDLE     hInstance, hPrevInstance;
  18. LPSTR      lpszCmdLine;
  19. int        cmdShow;
  20.   {
  21.   HWND      hWnd;
  22.   WNDCLASS  rClass;
  23.   MSG       msg;
  24.  
  25.   if (!hPrevInstance)
  26.     {
  27.     rClass.style         = CS_HREDRAW | CS_VREDRAW;
  28.     rClass.lpfnWndProc   = WndProc;
  29.     rClass.cbClsExtra    = 0;
  30.     rClass.cbWndExtra    = 0;
  31.     rClass.hInstance     = hInstance;
  32.     rClass.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
  33.     rClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  34.     rClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  35.     rClass.lpszMenuName  = NULL;
  36.     rClass.lpszClassName = "CLIPBOX";
  37.  
  38.     if (!RegisterClass (&rClass))
  39.       return FALSE;
  40.     }
  41.  
  42.   hWnd = CreateWindow ("CLIPBOX",
  43.       "GetClipBox",
  44.       WS_OVERLAPPEDWINDOW,
  45.       CW_USEDEFAULT,
  46.       0,
  47.       CW_USEDEFAULT,
  48.       0,
  49.       NULL,
  50.       NULL,
  51.       hInstance,
  52.       NULL);
  53.  
  54.   ShowWindow (hWnd, cmdShow);
  55.   UpdateWindow (hWnd);
  56.   hDC = GetDC (hWnd);
  57.  
  58.   while (GetMessage (&msg, NULL, 0, 0))
  59.     {
  60.     TranslateMessage (&msg);
  61.     DispatchMessage (&msg);
  62.     }
  63.   return (msg.wParam);
  64.   }
  65.  
  66.  
  67. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  68. HWND     hWnd;
  69. unsigned iMessage;
  70. WORD     wParam;
  71. LONG     lParam;
  72.   {
  73.   HMENU  hMenu;
  74.   HRGN   hRgnBox;
  75.   HRGN   hRgnCircle;
  76.   HRGN   hRgnBoth;
  77.   RECT   lpRect;
  78.   short  nRgnType;
  79.  
  80.   switch (iMessage)
  81.     {
  82.     case WM_CREATE:
  83.       hMenu = CreateMenu ();
  84.       ChangeMenu (hMenu, NULL, (LPSTR)"CheckType", 100, MF_APPEND);
  85.       ChangeMenu (hMenu, NULL, (LPSTR)"Set Complex", 200, MF_APPEND);
  86.       ChangeMenu (hMenu, NULL, (LPSTR)"Set Simple", 300, MF_APPEND);
  87.       ChangeMenu (hMenu, NULL, (LPSTR)"Set Null", 400, MF_APPEND);
  88.       SetMenu (hWnd, hMenu);
  89.       break;
  90.  
  91.   case WM_COMMAND:
  92.     switch (wParam)
  93.       {
  94.       case 100:
  95.         nRgnType = GetClipBox (hDC, (LPRECT) & lpRect);
  96.         switch (nRgnType)
  97.           {
  98.           case ERROR:
  99.             MessageBox (hWnd, (LPSTR)"Device context is not valid",
  100.                 (LPSTR)"GetClipBox", MB_OK | MB_ICONEXCLAMATION);
  101.             break;
  102.           case NULLREGION:
  103.             MessageBox (hWnd, (LPSTR)"Clipping region is empty",
  104.                 (LPSTR)"GetClipBox", MB_OK);
  105.             break;
  106.           case SIMPLEREGION:
  107.             MessageBox (hWnd, (LPSTR)"Clipping region is simple",
  108.                 (LPSTR)"GetClipBox", MB_OK);
  109.             break;
  110.           case COMPLEXREGION:
  111.             MessageBox (hWnd, (LPSTR)"Clipping region is complexed",
  112.                 (LPSTR)"GetClipBox", MB_OK);
  113.             break;
  114.           }
  115.         InvalidateRect (hWnd, (LPRECT)NULL, TRUE);
  116.         break;
  117.  
  118.       case 200:
  119.         hRgnBox = CreateRectRgn (50, 75, 125, 150);
  120.         hRgnCircle = CreateEllipticRgn (50, 25, 150, 125);
  121.         hRgnBoth = CreateRectRgn (0, 0, 0, 0);
  122.         CombineRgn (hRgnBoth, hRgnBox, hRgnCircle, RGN_XOR);
  123.         SelectClipRgn (hDC, hRgnBoth);
  124.         break;
  125.  
  126.       case 300:
  127.         hRgnBox = CreateRectRgn (200, 225, 225, 250);
  128.         SelectClipRgn (hDC, hRgnBox);
  129.         break;
  130.  
  131.       case 400:
  132.         hRgnBox = CreateRectRgn (0, 0, 0, 0);
  133.         SelectClipRgn (hDC, hRgnBox);
  134.         break;
  135.       }
  136.     break;
  137.  
  138.     case WM_DESTROY:
  139.       ReleaseDC (hWnd, hDC);
  140.       PostQuitMessage (0);
  141.       break;
  142.  
  143.     default:
  144.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  145.     }
  146.   return (0L);
  147.   }
  148.