home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / icliprec.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  5KB  |  181 lines

  1. /*
  2.  *
  3.  *  IntersectClipRect
  4.  *
  5.  *  This program demonstrates the use of the IntersectClipRect function.
  6.  *  IntersectClipRect creates a new clipping region for the specified DC
  7.  *  by taking the intersection of the current clipping region and the
  8.  *  rectangle represented by the four values sent it. IntersectClipRect
  9.  *  is called from FunctionDemonstrated in this sample application.
  10.  */
  11.  
  12. #include <windows.h>
  13. HWND hWnd;
  14.  
  15. BOOL FAR PASCAL InitMaster (HANDLE, HANDLE, int);
  16. long FAR PASCAL MasterWindowProc (HANDLE, unsigned, WORD, LONG);
  17. void FAR PASCAL FunctionDemonstrated ();
  18.  
  19. /***********************   main procedure   *******************************/
  20.  
  21. int     PASCAL WinMain  (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  22. HANDLE hInstance, hPrevInstance;
  23. LPSTR  lpszCmdLine;
  24. int    cmdShow;
  25.   {
  26.   MSG  msg;
  27.  
  28.   InitMaster (hInstance, hPrevInstance, cmdShow);
  29.  
  30.   FunctionDemonstrated ();
  31.  
  32.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  33.     {
  34.     TranslateMessage ( (LPMSG) & msg);
  35.     DispatchMessage ( (LPMSG) & msg);
  36.     }
  37.   exit (msg.wParam);
  38.   }
  39.  
  40.  
  41. /*******************************   initialization   ***********************/
  42.  
  43. BOOL FAR PASCAL InitMaster (hInstance, hPrevInstance, cmdShow)
  44. HANDLE hInstance;
  45. HANDLE hPrevInstance;
  46. int    cmdShow;
  47.   {
  48.   WNDCLASS  wcMasterClass;
  49.   HMENU     hMenu;
  50.  
  51.   wcMasterClass.lpszClassName = (LPSTR) "Master";
  52.   wcMasterClass.hInstance     = hInstance;
  53.   wcMasterClass.lpfnWndProc   = MasterWindowProc;
  54.   wcMasterClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  55.   wcMasterClass.hIcon         = NULL;
  56.   wcMasterClass.lpszMenuName  = (LPSTR) NULL;
  57.   wcMasterClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  58.   wcMasterClass.style         = CS_HREDRAW | CS_VREDRAW;
  59.   wcMasterClass.cbClsExtra    = 0;
  60.   wcMasterClass.cbWndExtra    = 0;
  61.  
  62.   RegisterClass ( (LPWNDCLASS) & wcMasterClass);
  63.  
  64.   hMenu = CreateMenu ();
  65.   ChangeMenu (hMenu, NULL, "Execute Function", 100, MF_APPEND);
  66.  
  67.   hWnd = CreateWindow ( (LPSTR) "Master", (LPSTR) "IntersectClipRect",
  68.                       WS_OVERLAPPEDWINDOW,
  69.                       CW_USEDEFAULT, 0,
  70.                       CW_USEDEFAULT, 0,
  71.                       NULL,hMenu , hInstance, NULL);
  72.  
  73.   ShowWindow (hWnd, cmdShow);
  74.   UpdateWindow (hWnd);
  75.  
  76.   return TRUE;
  77.   }
  78.  
  79. /*********************   window procedure - process messages   *************/
  80. long    FAR PASCAL MasterWindowProc (hWnd, message, wParam, lParam)
  81. HWND        hWnd;
  82. unsigned    message;
  83. WORD        wParam;
  84. LONG        lParam;
  85.   {
  86.   switch (message)
  87.     {
  88.     case WM_COMMAND:
  89.       if (wParam == 100)
  90.         FunctionDemonstrated ();
  91.       else
  92.         return (DefWindowProc (hWnd, message, wParam, lParam));
  93.       break;
  94.  
  95.     case WM_DESTROY:
  96.       PostQuitMessage (0);
  97.       break;
  98.  
  99.     default:
  100.       return (DefWindowProc (hWnd, message, wParam, lParam));
  101.       break;
  102.     }
  103.   return (0L);
  104.   }
  105.  
  106. /****************************   function demonstrated   ********************/
  107.  
  108. void FAR PASCAL FunctionDemonstrated ()
  109.   {
  110.   HDC hDC = GetDC (hWnd);
  111.   HRGN hRgn;         /* handle to first clipping region    */
  112.   RECT ClientRect;
  113.   POINT RectPoint[2];
  114.   short    nNewClipRect;     /* return value from IntersectClipRect */
  115.  
  116.   GetClientRect (hWnd, (LPRECT) & ClientRect); /* load device coordinates */
  117.  
  118.   SetMapMode (hDC, MM_ANISOTROPIC);
  119.   SetWindowOrg (hDC, 0, 0);
  120.   SetWindowExt (hDC, 120, 120);
  121.   SetViewportOrg (hDC, 0, 0);
  122.   SetViewportExt (hDC, (short) ClientRect.right, (short) ClientRect.bottom);
  123.  
  124.   RectPoint[0].x = 0;
  125.   RectPoint[0].y = 0;
  126.   RectPoint[1].x = 100;
  127.   RectPoint[1].y = 100;
  128.  
  129.   LPtoDP (hDC, (LPPOINT) & RectPoint[0].x, 2);
  130.  
  131.   hRgn = CreateRectRgn ( (short) RectPoint[0].x,
  132.       (short) RectPoint[0].y,
  133.       (short) RectPoint[1].x,
  134.       (short) RectPoint[1].y);
  135.  
  136.   SelectClipRgn (hDC, hRgn);
  137.  
  138.   MessageBox (GetFocus (), (LPSTR)"into the first clipping region",
  139.       (LPSTR)"I am about to do a TextOut and InvertRect...",
  140.       MB_OK);
  141.  
  142.   TextOut (hDC, 0, 5,
  143.       "THIS LINE HERE IS VERY LONG . WILL IT ALL BE SHOWN IN THE CLIPPING REGION?",
  144.       (short)75);
  145.   InvertRect (hDC, (LPRECT) & ClientRect);
  146.  
  147. /*** call IntersectClipRect with first clipping region and new coordinates ***/
  148.  
  149.   RectPoint[0].x = 5;
  150.   RectPoint[0].y = 5;
  151.   RectPoint[1].x = 20;
  152.   RectPoint[1].y = 20;
  153.  
  154.   LPtoDP (hDC, (LPPOINT) & RectPoint[0].x, 2);
  155.  
  156.   nNewClipRect = IntersectClipRect (hDC,
  157.       (short)RectPoint[0].x,
  158.       (short)RectPoint[0].y,
  159.       (short)RectPoint[1].x,
  160.       (short)RectPoint[1].y);
  161.  
  162.   if (nNewClipRect == ERROR)
  163.     MessageBox (GetFocus (), (LPSTR)"The DC sent IntersectClipRect is not valid",
  164.         (LPSTR)NULL,
  165.         MB_OK);
  166.  
  167.   MessageBox (GetFocus (), (LPSTR)"into the intersected clipping region",
  168.       (LPSTR)"I am about to do a TextOut and InvertRect...",
  169.       MB_OK);
  170.  
  171.   TextOut (hDC, 0, 15,
  172.       "THIS LINE HERE IS VERY LONG . WILL IT ALL BE SHOWN IN THE CLIPPING REGION?",
  173.       (short)75);
  174.   InvertRect (hDC, (LPRECT) & ClientRect);
  175.  
  176.   ReleaseDC (hWnd, hDC);
  177.   DeleteObject (hRgn);
  178.  
  179.   return;
  180.   }
  181.