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

  1. /*
  2.  * Function (s) demonstrated in this program: OffsetClipRgn
  3.  * Compiler version: C 5.1
  4.  * Description: This program creates a rectangle region, gets it location,
  5.  *              frames the region, offsets the region, displays what kind
  6.  *              of region it is, get it new loction of the region, frames
  7.  *              the new location, and displays the difference of the offset.
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include "offclipr.h"
  13.  
  14. static char    szFuncName[] = "OffsetClipRgn";
  15. static char    szAppName[] = "OFFCLIPR";
  16.  
  17. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  18. HANDLE     hInstance, hPrevInstance;
  19. LPSTR      lpszCmdLine;
  20. int        nCmdShow;
  21.   {
  22.   HWND        hWnd;
  23.   WNDCLASS    wndclass;
  24.   MSG         msg;
  25.   HMENU       hMenu;
  26.  
  27.   if (!hPrevInstance)
  28.     {
  29.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  30.     wndclass.lpfnWndProc   = WndProc;
  31.     wndclass.cbClsExtra    = 0;
  32.     wndclass.cbWndExtra    = 0;
  33.     wndclass.hInstance     = hInstance;
  34.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  35.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  36.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  37.     wndclass.lpszMenuName  = (LPSTR)"OFFCLIP";
  38.     wndclass.lpszClassName = szFuncName;
  39.  
  40.     if (!RegisterClass (&wndclass))
  41.       return FALSE;
  42.     }
  43.  
  44.   hMenu = LoadMenu (hInstance, (LPSTR)"OFFCLIP");
  45.  
  46.   hWnd = CreateWindow (szFuncName, szAppName,
  47.                       WS_OVERLAPPEDWINDOW,
  48.                       CW_USEDEFAULT, 0,
  49.                       CW_USEDEFAULT, 0,
  50.                       NULL, hMenu, hInstance, NULL);
  51.  
  52.   ShowWindow (hWnd, nCmdShow);
  53.   UpdateWindow (hWnd);
  54.  
  55.   while (GetMessage (&msg, NULL, 0, 0))
  56.     {
  57.     TranslateMessage (&msg);
  58.     DispatchMessage (&msg);
  59.     }
  60.   return (msg.wParam);
  61.   }
  62.  
  63. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  64. HWND     hWnd;
  65. unsigned iMessage;
  66. WORD     wParam;
  67. LONG     lParam;
  68.   {
  69.   HDC         hDC;
  70.   HRGN        hRgnBox;
  71.   HBRUSH      hBlueBrush;
  72.   HBRUSH      hRedBrush;
  73.   RECT        rectBefore;
  74.   RECT        rectAfter;
  75.   short    nRgnType;
  76.   int    nIndex;
  77.   char    szBuffer [300];
  78.  
  79.   switch (iMessage)
  80.     {
  81.     case WM_INITMENU:
  82.       InvalidateRect (hWnd, (LPRECT)NULL, TRUE);
  83.       break;
  84.  
  85.     case WM_COMMAND:
  86.       if (wParam == IDM_BOX)
  87.         {
  88.         hRgnBox = CreateRectRgn (50, 75, 125, 150);
  89.  
  90.         hBlueBrush = CreateSolidBrush (RGB (0, 0, 255));
  91.         hRedBrush = CreateSolidBrush (RGB (255, 0, 0));
  92.         if (hBlueBrush == NULL || hRedBrush == NULL)
  93.           {
  94.           MessageBox (GetFocus (), (LPSTR)"Error in Creating brushes.",
  95.               (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  96.           return (0L);
  97.           }
  98.  
  99.         hDC = GetDC (hWnd);
  100.  
  101.         nRgnType = SelectClipRgn (hDC, hRgnBox);
  102.         if (nRgnType == ERROR)
  103.           {
  104.           MessageBox (GetFocus (), (LPSTR)"Error in selecting clip region.",
  105.               (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  106.           return (0L);
  107.           }
  108.  
  109.         nRgnType = GetClipBox (hDC, (LPRECT) & rectBefore);
  110.         if (nRgnType == ERROR)
  111.           {
  112.           MessageBox (GetFocus (), (LPSTR)"Error in getting clip box.",
  113.               (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  114.           return (0L);
  115.           }
  116.         FrameRect (hDC, (LPRECT) & rectBefore, hBlueBrush);
  117.  
  118.         MessageBox (GetFocus (), (LPSTR)"Going to offset clip region.",
  119.             (LPSTR)szFuncName, MB_OK);
  120.  
  121.         nRgnType = OffsetClipRgn (hDC, 200, 50);
  122.         switch (nRgnType)
  123.           {
  124.           case COMPLEXREGION:
  125.             MessageBox (GetFocus (), (LPSTR)"Offset clip region is complex.",
  126.                 (LPSTR)szFuncName, MB_OK);
  127.             break;
  128.           case SIMPLEREGION:
  129.             MessageBox (GetFocus (), (LPSTR)"Offset clip region is simple.",
  130.                 (LPSTR)szFuncName, MB_OK);
  131.             break;
  132.           case NULLREGION:
  133.             MessageBox (GetFocus (), (LPSTR)"There is no offset clip region.",
  134.                 (LPSTR)szFuncName, MB_OK);
  135.             break;
  136.           case ERROR:
  137.             MessageBox (GetFocus (), (LPSTR)"Error in offset clip region.",
  138.                 (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  139.             break;
  140.           default :
  141.             MessageBox (GetFocus (), (LPSTR)"Error !!!!",
  142.                 (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  143.             break;
  144.           }
  145.  
  146.         nRgnType = GetClipBox (hDC, (LPRECT) & rectAfter);
  147.         if (nRgnType == ERROR)
  148.           {
  149.           MessageBox (GetFocus (), (LPSTR)"Error in getting clip box.",
  150.               (LPSTR)szFuncName, MB_OK | MB_ICONHAND);
  151.           return (0L);
  152.           }
  153.         FrameRect (hDC, (LPRECT) & rectAfter, hRedBrush);
  154.  
  155.         nIndex = sprintf (szBuffer,
  156.             "%s %d %s %d\n%s %d %s %d\n%s %d %s %d\n%s %d %s %d",
  157.             "clip box .left moved from", rectBefore.left,
  158.             "to", rectAfter.left,
  159.             "clip box .right moved from", rectBefore.right,
  160.             "to", rectAfter.right,
  161.             "clip box .top moved from", rectBefore.top,
  162.             "to", rectAfter.top,
  163.             "clip box .bottom moved from", rectBefore.bottom,
  164.             "to", rectAfter.bottom);
  165.         MessageBox (GetFocus (), (LPSTR)szBuffer, (LPSTR)szFuncName, MB_OK);
  166.  
  167.         ReleaseDC (hWnd, hDC);  /*  Clean up Memory  */
  168.         DeleteObject (hRgnBox);
  169.         DeleteObject (hRedBrush);
  170.         DeleteObject (hBlueBrush);
  171.         }
  172.       break;
  173.  
  174.     case WM_DESTROY:
  175.       PostQuitMessage (0);
  176.       break;
  177.  
  178.     default:
  179.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  180.     }
  181.   return (0L);
  182.   }
  183.