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

  1. /*
  2.  *  Function (s) demonstrated in this program: FillRect
  3.  *
  4.  *  Description:
  5.  *   This function will fill in a specified area of type RECT with the
  6.  *   designated brush.  The program below fills a red rectangle.
  7.  */
  8.  
  9. #include "windows.h"
  10.  
  11. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  12.  
  13. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  14. HANDLE    hInstance, hPrevInstance;
  15. LPSTR     lpszCmdLine;
  16. int       cmdShow;
  17.   {
  18.   HWND     hWnd;
  19.   WNDCLASS wcClass;
  20.   MSG      msg;
  21.  
  22.   if (!hPrevInstance)
  23.     {
  24.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  25.     wcClass.lpfnWndProc    = WndProc;
  26.     wcClass.cbClsExtra     = 0;
  27.     wcClass.cbWndExtra     = 0;
  28.     wcClass.hInstance      = hInstance;
  29.     wcClass.hIcon          = LoadIcon (hInstance, NULL);
  30.     wcClass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  31.     wcClass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  32.     wcClass.lpszMenuName   = NULL;
  33.     wcClass.lpszClassName  = "FILLRECT";
  34.  
  35.     if (!RegisterClass (&wcClass))
  36.       return FALSE;
  37.     }
  38.  
  39.   hWnd = CreateWindow ("FILLRECT",
  40.                       "FillRect",
  41.                       WS_OVERLAPPEDWINDOW,
  42.                       CW_USEDEFAULT,
  43.                       0,
  44.                       CW_USEDEFAULT,
  45.                       0,
  46.                       NULL,
  47.                       NULL,
  48.                       hInstance,
  49.                       NULL);
  50.  
  51.   ShowWindow (hWnd, cmdShow);
  52.   UpdateWindow (hWnd);
  53.  
  54.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  55.     {
  56.     TranslateMessage ( (LPMSG) & msg);
  57.     DispatchMessage ( (LPMSG) & msg);
  58.     }
  59.   return (msg.wParam);
  60.   }
  61.  
  62. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  63. HWND     hWnd;
  64. unsigned iMessage;
  65. WORD     wParam;
  66. LONG     lParam;
  67.   {
  68.   HDC    hDC;
  69.   HMENU  hMenu;
  70.   HBRUSH hBrush;
  71.   RECT   ShowRect;
  72.  
  73.   switch (iMessage)
  74.     {
  75.     case WM_CREATE:
  76.       hMenu = CreateMenu ();
  77.       ChangeMenu (hMenu, NULL, (LPSTR)"Fill", 100, MF_APPEND);
  78.       SetMenu (hWnd, hMenu);
  79.       break;
  80.  
  81.     case WM_INITMENU:
  82.       InvalidateRect (hWnd, NULL, TRUE);
  83.       break;
  84.  
  85.     case WM_COMMAND:
  86.       if (wParam == 100)
  87.         {
  88.         hDC = GetDC (hWnd);
  89.         hBrush = CreateSolidBrush (RGB (255, 0, 0));
  90.         ShowRect.left   = 50;
  91.         ShowRect.top    = 20;
  92.         ShowRect.right  = 400;
  93.         ShowRect.bottom = 100;
  94.         FillRect (hDC, (LPRECT) & ShowRect, hBrush);
  95.         DeleteObject (hBrush);
  96.         ReleaseDC (hWnd, hDC);
  97.         }
  98.       break;
  99.  
  100.     case WM_DESTROY:
  101.       PostQuitMessage (0);
  102.       break;
  103.  
  104.     default:
  105.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  106.       break;
  107.     }
  108.   return (0L);
  109.   }
  110.