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

  1. /*
  2.  *   IntersectRect
  3.  *
  4.  *   This example application demonstrates the use of the IntersectRect
  5.  *   function. The IntersectRect function finds the largest area common
  6.  *   to two RECT structures and puts the representation of the intersection
  7.  *   in the RECT structure defined by the first parameter in the call to
  8.  *   IntersectRect. IntersectRect is called twice in this application. The
  9.  *   first time it is called, there is no area common to the two rectangles,
  10.  *   the second time it is called, there is a common area.
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  16.  
  17. /* Procedure called when the application is loaded for the first time */
  18. BOOL HelloInit( hInstance )
  19. HANDLE hInstance;
  20. {
  21.     PWNDCLASS   pHelloClass;
  22.  
  23.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  24.  
  25.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  26.     pHelloClass->hIcon          = LoadIcon( hInstance,NULL);
  27.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  28.     pHelloClass->lpszClassName  = (LPSTR)"Sample Application";
  29.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  30.     pHelloClass->hInstance      = hInstance;
  31.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  32.     pHelloClass->lpfnWndProc    = HelloWndProc;
  33.  
  34.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  35.         /* Initialization failed.
  36.          * Windows will automatically deallocate all allocated memory.
  37.          */
  38.         return FALSE;
  39.  
  40.     LocalFree( (HANDLE)pHelloClass );
  41.     return TRUE;        /* Initialization succeeded */
  42. }
  43.  
  44. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  45. HANDLE hInstance, hPrevInstance;
  46. LPSTR lpszCmdLine;
  47. int cmdShow;
  48. {
  49.     MSG   msg;
  50.     HWND  hWnd;
  51.     HMENU hMenu;
  52.  
  53.     HDC hDC;           /** handle to display context     **/
  54.     RECT lpRect1;       /** rectangle coordinates structure     **/
  55.     RECT lpRect2;       /** rectangle coordinates structure     **/
  56.     RECT lpRect3;       /** rectangle coordinates structure     **/
  57.     RECT destRect;       /** target rectangle coord structure  **/
  58.  
  59.     HelloInit( hInstance );
  60.     hWnd = CreateWindow((LPSTR)"Sample Application",
  61.                         (LPSTR)"Sample Application",
  62.             WS_OVERLAPPEDWINDOW,
  63.             CW_USEDEFAULT,
  64.             CW_USEDEFAULT,
  65.             CW_USEDEFAULT,
  66.             CW_USEDEFAULT,
  67.             (HWND)NULL,       /* no parent */
  68.                         (HMENU)NULL,       /* use class menu */
  69.                         (HANDLE)hInstance, /* handle to window instance */
  70.                         (LPSTR)NULL        /* no params to pass on */
  71.                         );
  72.  
  73.     /* Make window visible according to the way the app is activated */
  74.     ShowWindow( hWnd, cmdShow );
  75.     UpdateWindow( hWnd );
  76.  
  77. /** get handle to display context **/
  78.    hDC = GetDC(hWnd);
  79.  
  80. /** fill lpRect1 with coordinate info for rectangle **/
  81.    lpRect1.left = 10;
  82.    lpRect1.top    = 10;
  83.    lpRect1.right= 150;
  84.    lpRect1.bottom=150;
  85.  
  86. /** fill lpRect2 with coordinate info for rectangle **/
  87.    lpRect2.left = 160;
  88.    lpRect2.top    = 10;
  89.    lpRect2.right= 210;
  90.    lpRect2.bottom=50;
  91.  
  92. /** fill lpRect3 with coordinate info for rectangle **/
  93.    lpRect3.left = 30;
  94.    lpRect3.top    = 30;
  95.    lpRect3.right= 200;
  96.    lpRect3.bottom=200;
  97.  
  98.    MessageBox(hWnd,(LPSTR)"three rectangles",
  99.           (LPSTR)"I am about to InvertRect...",MB_OK);
  100.  
  101. /** invert pixels in lpRect1 **/
  102.    InvertRect (hDC,(LPRECT)&lpRect1);
  103.  
  104. /** invert pixels in lpRect2 **/
  105.    InvertRect (hDC,(LPRECT)&lpRect2);
  106.  
  107. /** invert pixels in lpRect3 **/
  108.    InvertRect (hDC,(LPRECT)&lpRect3);
  109.  
  110.    TextOut(hDC,60,0,(LPSTR)"RECT 1",(int)6);
  111.    TextOut(hDC,160,0,(LPSTR)"RECT 2",(int)6);
  112.    TextOut(hDC,90,210,(LPSTR)"RECT 3",(int)6);
  113.  
  114.    MessageBox(hWnd,(LPSTR)"the intersection of rects 1 and 2",
  115.           (LPSTR)"I am about to InvertRect...",MB_OK);
  116.  
  117. /** find intersection of lpRect1 and lpRect2 and place in destRect  **/
  118.    IntersectRect((LPRECT)&destRect,(LPRECT)&lpRect1,(LPRECT)&lpRect2);
  119.  
  120. /** invert the intersection of lpRect1 and lpRect2 (should be empty) **/
  121.    InvertRect (hDC,(LPRECT)&destRect);
  122.  
  123.    MessageBox(hWnd,(LPSTR)"the intersection of rects 1 and 3",
  124.           (LPSTR)"I am about to InvertRect...",MB_OK);
  125.  
  126. /** find intersection of lpRect1 and lpRect3 and place in destRect  **/
  127.    IntersectRect((LPRECT)&destRect,(LPRECT)&lpRect1,(LPRECT)&lpRect3);
  128.  
  129. /** invert the intersection of lpRect1 and lpRect2            **/
  130.    InvertRect (hDC,(LPRECT)&destRect);
  131.  
  132.     /* Polling messages from event queue */
  133.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  134.         TranslateMessage((LPMSG)&msg);
  135.         DispatchMessage((LPMSG)&msg);
  136.         }
  137.  
  138.     return (int)msg.wParam;
  139. }
  140.  
  141.  
  142. /* Procedures which make up the window class. */
  143. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  144. HWND hWnd;
  145. unsigned message;
  146. WORD wParam;
  147. LONG lParam;
  148. {
  149.     PAINTSTRUCT ps;
  150.  
  151.     switch (message)
  152.     {
  153.     case WM_SYSCOMMAND:
  154.     return DefWindowProc( hWnd, message, wParam, lParam );
  155.         break;
  156.  
  157.     case WM_DESTROY:
  158.         PostQuitMessage( 0 );
  159.         break;
  160.  
  161.     case WM_PAINT:
  162.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  163.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  164.         break;
  165.  
  166.     default:
  167.         return DefWindowProc( hWnd, message, wParam, lParam );
  168.         break;
  169.     }
  170.     return(0L);
  171. }
  172.