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

  1. /*
  2.  * Function (s) demonstrated in this program: FrameRect
  3.  * Compiler version: C 5.1
  4.  *
  5.  * Description:
  6.  *    FrameRect draws a border around a specified rectangle using
  7.  *    a specified brush.
  8.  */
  9.  
  10. #include "windows.h"
  11.  
  12. int     PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  13. BOOL FrameRectInit (HANDLE);
  14. long    FAR PASCAL FrameRectWndProc (HWND, unsigned, WORD, LONG);
  15.  
  16. HANDLE hInst;
  17.  
  18. /**************************************************************************/
  19.  
  20. int     PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  21. HANDLE hInstance;           /* current instance             */
  22. HANDLE hPrevInstance;       /* previous instance            */
  23. LPSTR lpCmdLine;            /* command line                 */
  24. int     nCmdShow;               /* show-window type (open/icon) */
  25.   {
  26.   HWND hWnd;              /* window handle                */
  27.   MSG msg;                /* message                      */
  28.  
  29.   if (!hPrevInstance)     /* Has application been initialized? */
  30.     if (!FrameRectInit (hInstance))
  31.       return (NULL);  /* Exits if unable to initialize     */
  32.  
  33.   hInst = hInstance;      /* Saves the current instance        */
  34.  
  35.   hWnd = CreateWindow ("FrameRect",   /* window class            */
  36.                       "FrameRect Sample Application", /* window name             */
  37.                       WS_OVERLAPPEDWINDOW,           /* window style            */
  38.                       CW_USEDEFAULT,                 /* x position              */
  39.                       CW_USEDEFAULT,                 /* y position              */
  40.                       CW_USEDEFAULT,                 /* width                   */
  41.                       CW_USEDEFAULT,                 /* height                  */
  42.                       NULL,                          /* parent handle           */
  43.                       NULL,                          /* menu or child ID        */
  44.                       hInstance,                     /* instance                */
  45.                       NULL);                         /* additional info         */
  46.  
  47.   if (!hWnd)
  48.     return (NULL);
  49.  
  50.   ShowWindow (hWnd, nCmdShow);        /* Shows the window        */
  51.   UpdateWindow (hWnd);                /* Sends WM_PAINT message  */
  52.  
  53.   while (GetMessage (&msg, NULL, NULL, NULL))
  54.     {
  55.     TranslateMessage (&msg);
  56.     DispatchMessage (&msg);
  57.     }
  58.   return (msg.wParam);
  59.   }
  60.  
  61.  
  62. /*************************************************************************/
  63.  
  64. BOOL FrameRectInit (hInstance)
  65. HANDLE hInstance;
  66.   {
  67.   HANDLE hMemory;
  68.   PWNDCLASS pWndClass;
  69.   BOOL bSuccess;
  70.  
  71.   hMemory = LocalAlloc (LPTR, sizeof (WNDCLASS));
  72.   pWndClass = (PWNDCLASS) LocalLock (hMemory);
  73.  
  74.   pWndClass->style = NULL;
  75.   pWndClass->lpfnWndProc = FrameRectWndProc;
  76.   pWndClass->hInstance = hInstance;
  77.   pWndClass->hIcon = NULL;
  78.   pWndClass->hCursor = LoadCursor (NULL, IDC_ARROW);
  79.   pWndClass->hbrBackground = GetStockObject (WHITE_BRUSH);
  80.   pWndClass->lpszMenuName = (LPSTR) "FrameRectMenu";
  81.   pWndClass->lpszClassName = (LPSTR) "FrameRect";
  82.  
  83.   bSuccess = RegisterClass (pWndClass);
  84.  
  85.   LocalUnlock (hMemory);
  86.   LocalFree (hMemory);
  87.  
  88.   return (bSuccess);
  89.   }
  90.  
  91.  
  92. /**************************************************************************/
  93.  
  94. long    FAR PASCAL FrameRectWndProc (hWnd, message, wParam, lParam)
  95. HWND      hWnd;
  96. unsigned  message;
  97. WORD      wParam;
  98. LONG      lParam;
  99.   {
  100.   FARPROC lpProcAbout;        /* pointer to the "About" function */
  101.   RECT  rRect;                /* Will hold client rectangle  */
  102.   HANDLE hDC;                 /* Handle to the display context  */
  103.   PAINTSTRUCT ps;             /* Paint Structure  */
  104.   HBRUSH hPaintBrush;         /* Brush to draw border with  */
  105.  
  106.   switch (message)
  107.     {
  108.     case WM_SIZE:
  109.     case WM_PAINT:
  110.       GetClientRect (hWnd, (LPRECT) & rRect); /*  Structure holding  */
  111.       InvalidateRect (hWnd, (LPRECT) & rRect, TRUE);
  112.         /*  Erase the background  */
  113.       hDC = BeginPaint (hWnd, &ps);   /*  Get the display context  */
  114.         /*  Make a border inside of the window  */
  115.       rRect.right -= 10;
  116.       rRect.left += 10;
  117.       rRect.top += 10;
  118.       rRect.bottom -= 10;
  119.       hPaintBrush = GetStockObject (BLACK_BRUSH);
  120.         /* Get a dark gray brush */
  121.       FrameRect (hDC, (LPRECT) & rRect, hPaintBrush);
  122.         /* Draw the border with a BLACK_BRUSH */
  123.       EndPaint (hWnd, &ps);
  124.       break;
  125.  
  126.     case WM_DESTROY:
  127.       PostQuitMessage (0);
  128.       break;
  129.  
  130.     default:
  131.       return (DefWindowProc (hWnd, message, wParam, lParam));
  132.     }
  133.   return (NULL);
  134.   }
  135.