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

  1. /*
  2.  *   This program demonstrates the use of the CreateBrushIndirect () function.
  3.  *   CreateBrushIndirect () returns a handle to a brush with characteristics
  4.  *   described by a LOGBRUSH structure. CreateBrushIndirect () is called in
  5.  *   response to a WM_CREATE message in HelloWndProc (). The brush is used
  6.  *   to fill a rectangle in response to a WM_PAINT message.
  7.  *
  8.  */
  9.  
  10. #include    <windows.h>
  11. HBRUSH      hMyBrush;
  12. LOGBRUSH    lbBrushStyle;
  13. RECT        MyRect;
  14.  
  15. long    FAR PASCAL HelloWndProc (HWND, unsigned, WORD, LONG);
  16.  
  17. /* Procedure called when the application is loaded for the first time */
  18. BOOL HelloInit (hInstance)
  19. HANDLE hInstance;
  20.   {
  21.   PWNDCLASS   pHelloClass;
  22.  
  23.   pHelloClass = (PWNDCLASS)LocalAlloc (LPTR, sizeof (WNDCLASS));
  24.  
  25.   pHelloClass->hCursor        = LoadCursor (NULL, IDC_ARROW);
  26.   pHelloClass->hIcon             = LoadIcon (hInstance, NULL);
  27.   pHelloClass->lpszMenuName   = (LPSTR)NULL;
  28.   pHelloClass->lpszClassName     = (LPSTR)"CreateBrushIndirect";
  29.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  30.   pHelloClass->hInstance      = hInstance;
  31.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  32.   pHelloClass->lpfnWndProc    = HelloWndProc;
  33.  
  34.   if (!RegisterClass ( (LPWNDCLASS)pHelloClass))
  35.     return FALSE;
  36.  
  37.   LocalFree ( (HANDLE)pHelloClass);
  38.   return TRUE;        /* Initialization succeeded */
  39.   }
  40.  
  41.  
  42. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  43. HANDLE    hInstance, hPrevInstance;
  44. LPSTR     lpszCmdLine;
  45. int       cmdShow;
  46.   {
  47.   MSG   msg;
  48.   HWND  hWnd;
  49.  
  50.   HelloInit (hInstance);
  51.   hWnd = CreateWindow ( (LPSTR)"CreateBrushIndirect",
  52.                       (LPSTR)"CreateBrushIndirect ()",
  53.                       WS_OVERLAPPEDWINDOW,
  54.                       CW_USEDEFAULT,
  55.                       CW_USEDEFAULT,
  56.                       CW_USEDEFAULT,
  57.                       CW_USEDEFAULT,
  58.                       (HWND)NULL,        /* no parent */
  59.                       (HMENU)NULL,       /* use class menu */
  60.                       (HANDLE)hInstance, /* handle to window instance */
  61.                       (LPSTR)NULL );       /* no params to pass on */
  62.  
  63.   ShowWindow (hWnd, cmdShow);
  64.   UpdateWindow (hWnd);
  65.  
  66.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  67.     {
  68.     TranslateMessage ( (LPMSG) & msg);
  69.     DispatchMessage ( (LPMSG) & msg);
  70.     }
  71.   return (int)msg.wParam;
  72.   }
  73.  
  74. /* Procedures which make up the window class. */
  75. long    FAR PASCAL HelloWndProc (hWnd, message, wParam, lParam)
  76. HWND hWnd;
  77. unsigned    message;
  78. WORD wParam;
  79. LONG lParam;
  80.   {
  81.   PAINTSTRUCT ps;
  82.  
  83.   switch (message)
  84.     {
  85.     case WM_CREATE:
  86.           /*  initialize the LOGBRUSH structure "lbBrushStyle  */
  87.       lbBrushStyle.lbStyle = BS_HATCHED;
  88.       lbBrushStyle.lbColor = RGB (0x00, 0x00, 0x00);
  89.       lbBrushStyle.lbHatch = HS_BDIAGONAL;
  90.           /* initialize the rectangle structure "MyRect" */
  91.       MyRect.left   =  17;
  92.       MyRect.top    =  17;
  93.       MyRect.right  = 300;
  94.       MyRect.bottom = 100;
  95.           /*** create a hatched brush described by lbBrushStyle ***/
  96.       hMyBrush = CreateBrushIndirect ( (LPLOGBRUSH) & lbBrushStyle);
  97.       break;
  98.  
  99.     case WM_DESTROY:
  100.       PostQuitMessage (0);
  101.       break;
  102.  
  103.     case WM_PAINT:
  104.       BeginPaint (hWnd, (LPPAINTSTRUCT) & ps);
  105.       TextOut (ps.hdc, 5, 5,
  106.               (LPSTR)"This rectangle filled with hatched brush created via CreateBrushIndirect ()",
  107.               (long)74);
  108.       FillRect (ps.hdc, (LPRECT) & MyRect, hMyBrush);
  109.       EndPaint (hWnd, (LPPAINTSTRUCT) & ps);
  110.       break;
  111.  
  112.     default:
  113.       return DefWindowProc (hWnd, message, wParam, lParam);
  114.       break;
  115.     }
  116.   return (0L);
  117.   }
  118.