home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap19 / showbit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.7 KB  |  122 lines

  1. /*-----------------------------------------------------------
  2.    SHOWBIT.C -- Shows bitmaps in BITLIB dynamic link library
  3.                 (c) Charles Petzold, 1990
  4.   -----------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  9.  
  10. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  11.                     LPSTR lpszCmdLine, int nCmdShow)
  12.      {
  13.      static   char szAppName [] = "ShowBit" ;
  14.      HWND     hwnd ;
  15.      MSG      msg ;
  16.      WNDCLASS wndclass ;
  17.  
  18.      if (!hPrevInstance) 
  19.           {
  20.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.           wndclass.lpfnWndProc   = WndProc ;
  22.           wndclass.cbClsExtra    = 0 ;
  23.           wndclass.cbWndExtra    = 0 ;
  24.           wndclass.hInstance     = hInstance ;
  25.           wndclass.hIcon         = NULL ;
  26.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  28.           wndclass.lpszMenuName  = NULL ;
  29.           wndclass.lpszClassName = szAppName ;
  30.  
  31.           RegisterClass (&wndclass) ;
  32.           }
  33.  
  34.      hwnd = CreateWindow (szAppName, "Show Bitmaps from BITLIB (Press Key)",
  35.                           WS_OVERLAPPEDWINDOW,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           NULL, NULL, hInstance, NULL) ;
  39.  
  40.      ShowWindow (hwnd, nCmdShow) ;
  41.      UpdateWindow (hwnd) ;
  42.  
  43.      while (GetMessage (&msg, NULL, 0, 0))
  44.           {
  45.           TranslateMessage (&msg) ;
  46.           DispatchMessage (&msg) ;
  47.           }
  48.      return msg.wParam ;
  49.      }
  50.  
  51. void DrawBitmap (HDC hdc, short xStart, short yStart, HBITMAP hBitmap)
  52.      {
  53.      BITMAP bm ;
  54.      DWORD  dwSize ;
  55.      HDC    hMemDC ;
  56.      POINT  pt ;
  57.  
  58.      hMemDC = CreateCompatibleDC (hdc) ;
  59.      SelectObject (hMemDC, hBitmap) ;
  60.      GetObject (hBitmap, sizeof (BITMAP), (LPSTR) &bm) ;
  61.      pt.x = bm.bmWidth ;
  62.      pt.y = bm.bmHeight ;
  63.  
  64.      BitBlt (hdc, xStart, yStart, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY) ;
  65.  
  66.      DeleteDC (hMemDC) ;
  67.      }
  68.  
  69. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  70.      {
  71.      static HANDLE hLibrary ;
  72.      static short  nCurrent = 1 ;
  73.      HANDLE        hBitmap ;
  74.      HDC           hdc ;
  75.      PAINTSTRUCT   ps ;
  76.  
  77.      switch (message)
  78.           {
  79.           case WM_CREATE:
  80.                if ((hLibrary = LoadLibrary ("BITLIB.DLL")) < 32)
  81.                     MessageBeep (0) ;
  82.  
  83.                return 0 ;
  84.  
  85.           case WM_CHAR:
  86.                if (hLibrary >= 32)
  87.                     {
  88.                     nCurrent ++ ;
  89.                     InvalidateRect (hwnd, NULL, TRUE) ;
  90.                     }
  91.                return 0 ;
  92.  
  93.           case WM_PAINT:
  94.                hdc = BeginPaint (hwnd, &ps) ;
  95.  
  96.                if (hLibrary >= 32)
  97.                     {
  98.                     if (NULL == (hBitmap = LoadBitmap (hLibrary,
  99.                                              MAKEINTRESOURCE (nCurrent))))
  100.                          {
  101.                          nCurrent = 1 ;
  102.                          hBitmap = LoadBitmap (hLibrary,
  103.                                              MAKEINTRESOURCE (nCurrent)) ;
  104.                          }
  105.  
  106.                     if (hBitmap)
  107.                          DrawBitmap (hdc, 0, 0, hBitmap) ;
  108.                     }
  109.  
  110.                EndPaint (hwnd, &ps) ;
  111.                return 0 ;
  112.  
  113.           case WM_DESTROY:
  114.                if (hLibrary >= 32)
  115.                     FreeLibrary (hLibrary) ;
  116.  
  117.                PostQuitMessage (0) ;
  118.                return 0 ;
  119.           }
  120.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  121.      }
  122.