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

  1. /*
  2.  *  Function Name:   CreatePatternBrush
  3.  *  Program Name:    crpabrus.c
  4.  *  SDK Version:         2.03
  5.  *  Runtime Version:     2.03
  6.  *  Microsoft C Version: 5.0
  7.  *
  8.  *  Description:
  9.  *   This program takes a valid bitmap pattern and assigns it to be used as a
  10.  *   brush.  The program below draws a rectangle and fills it with a
  11.  *   crosshatch pattern brush created from a bitmap.
  12.  */
  13.  
  14. #include "windows.h"
  15.  
  16. long    FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  17.  
  18. /***********************************************************************/
  19.  
  20. void CALL_CreatePatternBrush(hWnd, hDC)
  21. HWND hWnd;
  22. HDC  hDC;
  23. {
  24.   HBRUSH    hBrush, hOldBrush;
  25.   HBITMAP   hBitLoad;
  26. /* set up 8x8 crosshatch pattern */
  27.   static short    BitmapArray[] = 
  28.   {
  29.     0xFF, 0xF7, 0xEB, 0xDD, 0xBE, 0x7F, 0xFF, 0xFF    };
  30.  
  31.   hBitLoad = CreateBitmap(8, 8, 1, 1, (LPSTR)BitmapArray); /* for monochrome */
  32.  
  33.   hBrush = CreatePatternBrush(hBitLoad);      /* load pattern brush    */
  34.  
  35.   if (hBrush == NULL)                /* checks for successful brush creation */
  36.   {
  37.     MessageBox(hWnd, (LPSTR)"CreatePatternBrush failed",
  38.         (LPSTR)"ERROR", MB_ICONHAND);
  39.     return;
  40.   }
  41.  
  42.   hOldBrush = (HBRUSH) SelectObject (hDC, (HANDLE) hBrush);
  43.   Rectangle (hDC, 10, 10, 300, 100);
  44.   hBrush = SelectObject (hDC, hOldBrush);
  45.   DeleteObject(hBrush);
  46.   DeleteObject(hBitLoad);
  47.  
  48.   return;
  49. }
  50.  
  51.  
  52. /**************************************************************************/
  53.  
  54. /* Procedure called when the application is loaded for the first time */
  55. BOOL WinInit( hInstance )
  56. HANDLE hInstance;
  57. {
  58.   WNDCLASS   wcClass;
  59.  
  60.   wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  61.   wcClass.lpfnWndProc    = WndProc;
  62.   wcClass.cbClsExtra     = 0;
  63.   wcClass.cbWndExtra     = 0;
  64.   wcClass.hInstance      = hInstance;
  65.   wcClass.hIcon          = LoadIcon( hInstance, NULL );
  66.   wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  67.   wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  68.   wcClass.lpszMenuName   = (LPSTR)NULL;
  69.   wcClass.lpszClassName  = (LPSTR)"CreatePatternBrush";
  70.  
  71.   if (!RegisterClass( (LPWNDCLASS) & wcClass ) )
  72. /* Initialization failed.
  73.          * Windows will automatically deallocate all allocated memory.
  74.          */
  75.     return FALSE;
  76.  
  77.   return TRUE;        /* Initialization succeeded */
  78. }
  79.  
  80.  
  81. int    PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  82. HANDLE hInstance, hPrevInstance;
  83. LPSTR lpszCmdLine;
  84. int    cmdShow;
  85. {
  86.   MSG   msg;
  87.   HWND  hWnd;
  88.   HMENU hMenu;
  89.  
  90.   if (!hPrevInstance)
  91.   {
  92. /* Call initialization procedure if this is the first instance */
  93.     if (!WinInit( hInstance ))
  94.       return FALSE;
  95.   }
  96.  
  97.   hWnd = CreateWindow((LPSTR)"CreatePatternBrush",
  98.       (LPSTR)"CreatePatternBrush()",
  99.       WS_OVERLAPPEDWINDOW,
  100.       CW_USEDEFAULT,
  101.       CW_USEDEFAULT,
  102.       CW_USEDEFAULT,
  103.       CW_USEDEFAULT,
  104.       (HWND)NULL,        /* no parent */
  105.   (HMENU)NULL,       /* use class menu */
  106.   (HANDLE)hInstance, /* handle to window instance */
  107.   (LPSTR)NULL        /* no params to pass on */
  108.   );
  109.  
  110. /* Make window visible according to the way the app is activated */
  111.   ShowWindow( hWnd, cmdShow );
  112.   UpdateWindow( hWnd );
  113.  
  114. /* Polling messages from event queue */
  115.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  116.   {
  117.     TranslateMessage((LPMSG) & msg);
  118.     DispatchMessage((LPMSG) & msg);
  119.   }
  120.  
  121.   return (int)msg.wParam;
  122. }
  123.  
  124.  
  125. /* Procedures which make up the window class. */
  126. long    FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  127. HWND hWnd;
  128. unsigned    message;
  129. WORD wParam;
  130. LONG lParam;
  131. {
  132.   PAINTSTRUCT ps;
  133.  
  134.   switch (message)
  135.   {
  136.  
  137.   case WM_PAINT:
  138.     BeginPaint( hWnd, (LPPAINTSTRUCT) & ps );
  139.     CALL_CreatePatternBrush(hWnd, ps.hdc);
  140.     ValidateRect(hWnd, (LPRECT) NULL);
  141.     EndPaint( hWnd, (LPPAINTSTRUCT) & ps );
  142.     break;
  143.  
  144.   case WM_DESTROY:
  145.     PostQuitMessage( 0 );
  146.     break;
  147.  
  148.   default:
  149.     return DefWindowProc( hWnd, message, wParam, lParam );
  150.     break;
  151.   }
  152.   return(0L);
  153. }
  154.  
  155.  
  156.