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

  1. /*
  2.  
  3. A handle to a display context is generated using the BeginPaint function.
  4. This handle is used to select a new brush into the current display context.
  5. Once this new brush is created, it is used to paint a grey backround within
  6. a rectangle.
  7.  
  8. */
  9.  
  10. #include <windows.h>
  11. #include <string.h>
  12.  
  13. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  14.  
  15. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  16. HANDLE      hInstance, hPrevInstance ;
  17. LPSTR       lpszCmdLine ;
  18. int    nCmdShow ;
  19. {
  20.   static char    szAppName [] = "PaintRgn" ;
  21.   HWND        hWnd ;
  22.   WNDCLASS    wndclass ;
  23.   MSG msg;
  24.  
  25.   if (!hPrevInstance)
  26.   {
  27.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  28.     wndclass.lpfnWndProc   = WndProc ;
  29.     wndclass.cbClsExtra    = 0 ;
  30.     wndclass.cbWndExtra    = 0 ;
  31.     wndclass.hInstance     = hInstance ;
  32.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  33.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  34.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  35.     wndclass.lpszMenuName  = NULL ;
  36.     wndclass.lpszClassName = szAppName ;
  37.  
  38.     if (!RegisterClass (&wndclass))
  39.       return FALSE ;
  40.   }
  41.  
  42.   hWnd = CreateWindow (szAppName, (LPSTR)"PaintRgn", 
  43.       WS_OVERLAPPEDWINDOW, 
  44.       CW_USEDEFAULT, 0,
  45.       CW_USEDEFAULT, 0,
  46.       NULL, NULL, hInstance, NULL);
  47.  
  48.   ShowWindow (hWnd, nCmdShow) ;
  49.   UpdateWindow (hWnd) ;
  50.  
  51.   while (GetMessage(&msg, NULL, 0, 0))
  52.   {
  53.     TranslateMessage(&msg);
  54.     DispatchMessage(&msg);
  55.   }
  56.   return (msg.wParam) ;
  57. }
  58.  
  59.  
  60. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  61. HWND     hWnd ;
  62. unsigned    iMessage ;
  63. WORD     wParam ;
  64. LONG     lParam ;
  65. {
  66.   HDC      hDC;
  67.   HRGN     hRgn;
  68.   short    xX,                                      /* X coordinate */
  69.   yY;                                      /* Y coordinate */
  70.  
  71.   PAINTSTRUCT ps;
  72.   switch (iMessage)
  73.   {
  74.   case WM_PAINT:
  75.     {
  76.       hDC = BeginPaint(hWnd, (LPPAINTSTRUCT) & ps);
  77.  
  78.       TextOut (hDC, 10, 10,
  79.           (LPSTR)"The Windows function PaintRgn is used to fill the backround of the rectangle",
  80.           strlen("The Windows function PaintRgn is used to fill the backround of the rectangle")
  81.           );
  82.  
  83.       TextOut (hDC, 10, 22,
  84.           (LPSTR)"with the selected grey brush.",
  85.           strlen("with the selected grey brush.")
  86.           );
  87.  
  88.  
  89.       SelectObject (hDC, GetStockObject (GRAY_BRUSH));
  90.  
  91.       xX = GetSystemMetrics (SM_CXSCREEN);
  92.       yY = GetSystemMetrics (SM_CYSCREEN);
  93.  
  94.       hRgn = CreateRectRgn (xX / 4, yY / 4, xX / 2, yY / 2);
  95. /* PaintRgn fills the region specified by hRgn with the brush selected *
  96.     * into the display context.
  97.     */
  98.       PaintRgn (hDC, hRgn);
  99.       EndPaint(hWnd, (LPPAINTSTRUCT) & ps);
  100.       break;
  101.     }
  102.   case WM_DESTROY:
  103.     {
  104.       PostQuitMessage(0);
  105.       break;
  106.     }
  107.   default:
  108.     {
  109.       return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  110.     }
  111.   }
  112.   return (0L);
  113. }
  114.  
  115.  
  116.