home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / scroll / scrpat.c < prev    next >
Text File  |  1988-08-10  |  4KB  |  150 lines

  1. /*
  2.  *  Function(s) demonstrated in this program: UnrealizeObject, SetBrushOrg,
  3.  *                                            PatBlt
  4.  *  Compiler version: 5.1
  5.  *  Description:
  6.  *     This program will set up a cross hatched pattern on the client
  7.  *  area of a window.  It will then scroll it using the two above functions
  8.  *  in conjunction with timer messages.
  9.  */
  10.  
  11. #include <windows.h>
  12.  
  13. int     iTimerCount;
  14. HBRUSH  hNewBrush;
  15. RECT    rRect;
  16.  
  17. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  18.  
  19. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  20. HANDLE      hInstance, hPrevInstance ;
  21. LPSTR       lpszCmdLine ;
  22. int         nCmdShow ;
  23.   {
  24.   static char szAppName [] = "ScrPat" ;
  25.   HWND        hWnd ;
  26.   WNDCLASS    wndclass ;
  27.   MSG msg;
  28.   HDC hDC;
  29.  
  30.   if (!hPrevInstance)
  31.     {
  32.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  33.     wndclass.lpfnWndProc   = WndProc ;
  34.     wndclass.cbClsExtra    = 0 ;
  35.     wndclass.cbWndExtra    = 0 ;
  36.     wndclass.hInstance     = hInstance ;
  37.     wndclass.hIcon         = NULL;
  38.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  39.     wndclass.hbrBackground = NULL;
  40.     wndclass.lpszMenuName  = NULL ;
  41.     wndclass.lpszClassName = szAppName ;
  42.  
  43.     if (!RegisterClass (&wndclass))
  44.       return FALSE ;
  45.     }
  46.  
  47.   hWnd = CreateWindow (szAppName,            /* window class name       */
  48.                  "ScrPat Sample Application",  /* window caption          */
  49.                  WS_OVERLAPPEDWINDOW,        /* window style            */
  50.                  CW_USEDEFAULT,              /* initial x position      */
  51.                  0,                          /* initial y position      */
  52.                  CW_USEDEFAULT,              /* initial x size          */
  53.                  0,                          /* initial y size          */
  54.                  NULL,                       /* parent window handle    */
  55.                  NULL,                       /* window menu handle      */
  56.                  hInstance,                  /* program instance handle */
  57.                  NULL) ;                     /* create parameters       */
  58.  
  59.   /* Set up the background brush. */
  60.   hNewBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 0, 255));
  61.   hDC = GetDC(hWnd);
  62.   GetClientRect( hWnd, (LPRECT) &rRect );
  63.   ReleaseDC(hWnd, hDC);
  64.  
  65.   /* Start up the timer to roll the pattern. */
  66.   SetTimer(hWnd, 100, 133, 0L);
  67.  
  68.   ShowWindow (hWnd, nCmdShow) ;
  69.   UpdateWindow (hWnd) ;
  70.  
  71.   while (GetMessage(&msg, NULL, 0, 0))
  72.     {
  73.     TranslateMessage(&msg);
  74.     DispatchMessage(&msg);
  75.     } 
  76.   return (msg.wParam) ;     
  77.   }
  78.  
  79. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  80. HWND     hWnd ;
  81. unsigned iMessage ;
  82. WORD     wParam ;
  83. LONG     lParam ;
  84.   {
  85.   HDC         hDC;
  86.   HMENU       hMenu;
  87.   PAINTSTRUCT ps;
  88.  
  89.   switch(iMessage)
  90.     {
  91.     case WM_CREATE:
  92.       break;
  93.  
  94.     case WM_ERASEBKGND:
  95.         /* Don't let it paint the background. */
  96.         return((long)TRUE);
  97.  
  98.     case WM_PAINT:
  99.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  100.  
  101.         SelectObject(ps.hdc, GetStockObject(NULL_BRUSH));
  102.         UnrealizeObject(hNewBrush);
  103.         SetBrushOrg(ps.hdc, 0, iTimerCount);
  104.         SelectObject(ps.hdc, hNewBrush);
  105.  
  106.         PatBlt(ps.hdc,
  107.                ps.rcPaint.left,
  108.                ps.rcPaint.top,
  109.                ps.rcPaint.right - ps.rcPaint.left,
  110.                ps.rcPaint.bottom - ps.rcPaint.top,
  111.                PATCOPY);
  112.  
  113.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  114.         break;
  115.  
  116.     case WM_TIMER:
  117.         if (wParam == 100 ) {
  118.  
  119.             iTimerCount = (iTimerCount+1) % 64;
  120.  
  121.             hDC = GetDC(hWnd);
  122.  
  123.             SelectObject(hDC, GetStockObject(NULL_BRUSH));
  124.             UnrealizeObject(hNewBrush);
  125.             SetBrushOrg(hDC, 0, iTimerCount);
  126.             SelectObject(hDC, hNewBrush);
  127.  
  128.             PatBlt(hDC,
  129.                    rRect.left,
  130.                    rRect.top,
  131.                    rRect.right,
  132.                    rRect.bottom,
  133.                    PATCOPY);
  134.  
  135.             ReleaseDC(hWnd, hDC);
  136.  
  137.             break;
  138.             }
  139.  
  140.     case WM_DESTROY:
  141.       {
  142.       PostQuitMessage(0);
  143.       break;
  144.       }
  145.     default:
  146.       return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  147.     }
  148.     return (0L); 
  149.   }
  150.