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

  1. /*
  2.  *   IsRectEmpty
  3.  *
  4.  *   This example application demonstrates the use of the IsRectEmpty
  5.  *   function. IsRectEmpty returns a boolean specifying whether or not
  6.  *   the given rectangle is empty. A rectangle is empty if either the
  7.  *   height or width is 0. IsRectEmpty is called twice in WinMain in this
  8.  *   sample application. In the first call, IsRectEmpty is passed a non-
  9.  *   empty rectangle. In the second call, it is passed an empty rectangle.
  10.  */
  11.  
  12. #include "windows.h"
  13.  
  14. long    FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  15.  
  16. /* Procedure called when the application is loaded for the first time */
  17. BOOL HelloInit( hInstance )
  18. HANDLE hInstance;
  19. {
  20.   PWNDCLASS   pHelloClass;
  21.  
  22.   pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  23.  
  24.   pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  25.   pHelloClass->hIcon          = LoadIcon( hInstance, NULL);
  26.   pHelloClass->lpszMenuName   = (LPSTR)NULL;
  27.   pHelloClass->lpszClassName  = (LPSTR)"Sample Application";
  28.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  29.   pHelloClass->hInstance      = hInstance;
  30.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  31.   pHelloClass->lpfnWndProc    = HelloWndProc;
  32.  
  33.   if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  34. /* Initialization failed.
  35.          * Windows will automatically deallocate all allocated memory.
  36.          */
  37.     return FALSE;
  38.  
  39.   LocalFree( (HANDLE)pHelloClass );
  40.   return TRUE;        /* Initialization succeeded */
  41. }
  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.   BOOL EMPTY;        /** return value from IsRectEmpty calls **/
  57.  
  58.   HelloInit( hInstance );
  59.   hWnd = CreateWindow((LPSTR)"Sample Application", (LPSTR)"Sample Application",
  60.       WS_OVERLAPPEDWINDOW,
  61.       CW_USEDEFAULT, 0,
  62.       CW_USEDEFAULT, 0,
  63.       NULL,     NULL, hInstance, NULL);
  64.  
  65. /* Make window visible according to the way the app is activated */
  66.   ShowWindow( hWnd, cmdShow );
  67.   UpdateWindow( hWnd );
  68.  
  69. /** get handle to display context **/
  70.   hDC = GetDC(hWnd);
  71.  
  72. /** fill lpRect1 with coordinate info for non-empty rectangle **/
  73.   lpRect1.left = 10;
  74.   lpRect1.top     = 10;
  75.   lpRect1.right = 150;
  76.   lpRect1.bottom = 150;
  77.  
  78. /** fill lpRect2 with coordinate info for  empty rectangle **/
  79.   lpRect2.left = 260;
  80.   lpRect2.top     = 10;
  81.   lpRect2.right = 260;
  82.   lpRect2.bottom = 200;
  83.  
  84.   MessageBox(GetFocus(), (LPSTR)"the non-empty rectangle",
  85.       (LPSTR)"I am about to InvertRect...", MB_OK);
  86.  
  87. /** invert pixels in lpRect1  (the non-empty rectangle) **/
  88.   InvertRect (hDC, (LPRECT) & lpRect1);
  89.  
  90.   MessageBox(GetFocus(), (LPSTR)"with this non-empty rectangle",
  91.       (LPSTR)"I am about to call IsRectEmpty...", MB_OK);
  92.  
  93. /** see is lpRect1 is empty **/
  94.   EMPTY = IsRectEmpty((LPRECT) & lpRect1);
  95.   if (EMPTY)
  96.     MessageBox(GetFocus(), (LPSTR)"meaning the rectangle IS empty",
  97.         (LPSTR)"IsRectEmpty returned TRUE...", MB_OK);
  98.   else
  99.     MessageBox(GetFocus(), (LPSTR)"meaning the rectangle is NOT empty",
  100.         (LPSTR)"IsRectEmpty returned FALSE...", MB_OK);
  101.  
  102.   InvalidateRect(hWnd, NULL, TRUE);
  103.  
  104.   MessageBox(GetFocus(), (LPSTR)"the EMPTY rectangle",
  105.       (LPSTR)"I am about to InvertRect...", MB_OK);
  106.  
  107. /** invert pixels in lpRect2 (the empty rectangle) **/
  108.   InvertRect (hDC, (LPRECT) & lpRect2);
  109.  
  110.   MessageBox(GetFocus(), (LPSTR)"with this empty rectangle",
  111.       (LPSTR)"I am about to call IsRectEmpty...", MB_OK);
  112.  
  113. /** see is lpRect2 is empty **/
  114.   EMPTY = IsRectEmpty((LPRECT) & lpRect2);
  115.   if (EMPTY)
  116.     MessageBox(GetFocus(), (LPSTR)"meaning the rectangle IS empty",
  117.         (LPSTR)"IsRectEmpty returned TRUE...", MB_OK);
  118.   else
  119.     MessageBox(GetFocus(), (LPSTR)"meaning the rectangle is NOT empty",
  120.         (LPSTR)"IsRectEmpty returned FALSE...", MB_OK);
  121.  
  122. /* Polling messages from event queue */
  123.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  124.   {
  125.     TranslateMessage((LPMSG) & msg);
  126.     DispatchMessage((LPMSG) & msg);
  127.   }
  128.  
  129.   return (int)msg.wParam;
  130. }
  131.  
  132.  
  133. /* Procedures which make up the window class. */
  134. long    FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  135. HWND hWnd;
  136. unsigned    message;
  137. WORD wParam;
  138. LONG lParam;
  139. {
  140.   switch (message)
  141.   {
  142.   case WM_DESTROY:
  143.     PostQuitMessage( 0 );
  144.     break;
  145.  
  146.   default:
  147.     return DefWindowProc( hWnd, message, wParam, lParam );
  148.     break;
  149.   }
  150.   return(0L);
  151. }
  152.  
  153.  
  154.