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

  1. /*
  2.  * Function (s) demonstrated in this program: OffsetRgn,
  3.  *           BeginPaint, CreateRectRgn, EndPaint, GetSystemMetrics,
  4.  *           InvalidateRect, SelectObject, TextOut, PostQuitMessage
  5.  *
  6.  * This program demonstrates the use of the Windows function OffsetRgn.
  7.  * A handle to a rectangular region is generated by CreateRectRgn.  The
  8.  * contents of this region are filled with a solid backround.  The
  9.  * rectangle is initially located on one side of the client area.  When
  10.  * a button is pressed then the client area is erased and the rectangle
  11.  * is placed on the other side of the client area by the offsetrgn
  12.  * function.  This process can be repeated indefinitely.
  13.  *
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <string.h>
  18.  
  19. #define LEFT    0
  20. #define RIGHT   1
  21.  
  22. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  23. void WndPaint (HWND hWnd, HDC hDC, BOOL fPosition);
  24.  
  25. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  26. HANDLE     hInstance, hPrevInstance;
  27. LPSTR      lpszCmdLine;
  28. int        nCmdShow;
  29.   {
  30.   static char    szAppName [] = "OffsetRgn";
  31.   HWND          hWnd;
  32.   WNDCLASS      wndclass;
  33.   MSG           msg;
  34.  
  35.   if (!hPrevInstance)
  36.     {
  37.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  38.     wndclass.lpfnWndProc   = WndProc;
  39.     wndclass.cbClsExtra    = 0;
  40.     wndclass.cbWndExtra    = 0;
  41.     wndclass.hInstance     = hInstance;
  42.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  43.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  44.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  45.     wndclass.lpszMenuName  = NULL;
  46.     wndclass.lpszClassName = szAppName;
  47.  
  48.     if (!RegisterClass (&wndclass))
  49.       return FALSE;
  50.     }
  51.  
  52.   hWnd = CreateWindow (szAppName, (LPSTR) "OffsetRgn",
  53.                        WS_OVERLAPPEDWINDOW,
  54.                        CW_USEDEFAULT, 0,
  55.                        CW_USEDEFAULT, 0,
  56.                        NULL, NULL, hInstance, NULL);
  57.  
  58.   ShowWindow (hWnd, nCmdShow);
  59.   UpdateWindow (hWnd);
  60.  
  61.   while (GetMessage (&msg, NULL, 0, 0))
  62.     {
  63.     TranslateMessage (&msg);
  64.     DispatchMessage (&msg);
  65.     }
  66.   return msg.wParam;
  67.   }
  68.  
  69.  
  70. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  71. HWND     hWnd;
  72. unsigned iMessage;
  73. WORD     wParam;
  74. LONG     lParam;
  75.   {
  76.   HDC      hDC;                 /* For the paint routine */
  77.   PAINTSTRUCT ps;
  78.   RECT     Rect;
  79.   POINT    Point;
  80.   static BOOL fPosition = LEFT; /* The value determines the location of the
  81.                                  * rectangular region.  When LEFT then the
  82.                                  * the region is located toward the top of
  83.                                  * the window.  Otherwise the region is
  84.                                  * located toward the bottom of the window.
  85.                                  */
  86.   switch (iMessage)
  87.     {
  88.     case WM_PAINT:
  89.       hDC = BeginPaint (hWnd, (LPPAINTSTRUCT) & ps);
  90.       WndPaint (hWnd, hDC, fPosition);
  91.       EndPaint (hWnd, (LPPAINTSTRUCT) & ps);
  92.       break;
  93.  
  94.     case WM_KEYUP:
  95.       if (fPosition == LEFT)
  96.         fPosition = RIGHT;
  97.       else
  98.         fPosition = LEFT;
  99.  
  100.       GetWindowRect (hWnd, &Rect);
  101.  
  102.       /* Translate screen coordinates to client coordinates for the *
  103.        * InvalidateRect function.                                   */
  104.       Point.x = Rect.left;
  105.       Point.y = Rect.top;
  106.       ScreenToClient (hWnd, &Point);  /* Translate the upper left hand corner */
  107.       Rect.left = Point.x;
  108.       Rect.top = Point.y;
  109.  
  110.       Point.x = Rect.right;
  111.       Point.y = Rect.bottom;
  112.       ScreenToClient (hWnd, &Point); /* Translate the upper right hand corner */
  113.       Rect.right = Point.x;
  114.       Rect.bottom = Point.y;
  115.  
  116.       InvalidateRect (hWnd, &Rect, TRUE);
  117.       break;
  118.  
  119.     case WM_DESTROY:
  120.       PostQuitMessage (0);
  121.       break;
  122.  
  123.     default:
  124.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  125.     }
  126.   return (0L);
  127.   }
  128.  
  129. void WndPaint (hWnd, hDC, fPosition)
  130. HWND hWnd;
  131. HDC  hDC;
  132. BOOL fPosition;
  133.   {
  134.   HRGN        hRgn;
  135.   short X, Y;
  136.  
  137.   TextOut (hDC, 10, 10, "Pressing any key will cause the OffsetRgn function", 50);
  138.   TextOut (hDC, 10, 22, "to change the position of the rectangular region.", 49);
  139.   SelectObject (hDC, GetStockObject (GRAY_BRUSH));
  140.  
  141.   X = GetSystemMetrics (SM_CXSCREEN);
  142.   Y = GetSystemMetrics (SM_CYSCREEN);
  143.  
  144.   hRgn = CreateRectRgn (X / 4, Y / 4 + 50, X / 2, Y / 2 + 50);
  145.  
  146.   if (fPosition == LEFT)
  147. /* OffsetRgn moves the region identified by hRgn by the specified  *
  148.      * offsets.  The function moves the region 150 units along the     *
  149.      * x-axis and 0 units along the y-axis.                            */
  150.     OffsetRgn (hRgn, 150, 0);          /* Move rectangle region to the right */
  151.  
  152.   else
  153.     OffsetRgn (hRgn, -150, 0);          /* Move rectangle region to the left */
  154.  
  155. /* PaintRgn fills the region specified by hRgn with the brush selected *
  156.    * into the display context.                                           */
  157.  
  158.   PaintRgn (hDC, hRgn);
  159.   DeleteObject (hRgn);     /*  Delete region from memory  */
  160.   return;
  161.   }
  162.