home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / HelloBit / HelloBit.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.1 KB  |  134 lines

  1. /*-----------------------------------------
  2.    HELLOBIT.C -- Bitmap Demonstration
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName [] = TEXT ("HelloBit") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = szAppName ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("HelloBit"), 
  38.                           WS_OVERLAPPEDWINDOW, 
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56.      static HBITMAP hBitmap ;
  57.      static HDC     hdcMem ;
  58.      static int     cxBitmap, cyBitmap, cxClient, cyClient, iSize = IDM_BIG ;
  59.      static TCHAR * szText = TEXT (" Hello, world! ") ;
  60.      HDC            hdc ;
  61.      HMENU          hMenu ;
  62.      int            x, y ;
  63.      PAINTSTRUCT    ps ;
  64.      SIZE           size ;
  65.      
  66.      switch (message)
  67.      {
  68.      case WM_CREATE:
  69.           hdc = GetDC (hwnd) ;
  70.           hdcMem  = CreateCompatibleDC (hdc) ;
  71.  
  72.           GetTextExtentPoint32 (hdc, szText, lstrlen (szText), &size) ;
  73.           cxBitmap = size.cx ;
  74.           cyBitmap = size.cy ;
  75.           hBitmap = CreateCompatibleBitmap (hdc, cxBitmap, cyBitmap) ;
  76.  
  77.           ReleaseDC (hwnd, hdc) ;
  78.  
  79.           SelectObject (hdcMem, hBitmap) ;
  80.           TextOut (hdcMem, 0, 0, szText, lstrlen (szText)) ;
  81.           return 0 ;
  82.  
  83.      case WM_SIZE:
  84.           cxClient = LOWORD (lParam) ;
  85.           cyClient = HIWORD (lParam) ;
  86.           return 0 ;
  87.  
  88.      case WM_COMMAND:
  89.           hMenu = GetMenu (hwnd) ;
  90.  
  91.           switch (LOWORD (wParam))
  92.           {
  93.           case IDM_BIG:
  94.           case IDM_SMALL:
  95.                CheckMenuItem (hMenu, iSize, MF_UNCHECKED) ;
  96.                iSize = LOWORD (wParam) ;
  97.                CheckMenuItem (hMenu, iSize, MF_CHECKED) ;
  98.                InvalidateRect (hwnd, NULL, TRUE) ;
  99.                break ;
  100.           }
  101.           return 0 ;
  102.  
  103.      case WM_PAINT:
  104.           hdc = BeginPaint (hwnd, &ps) ;
  105.  
  106.           switch (iSize)
  107.           {
  108.           case IDM_BIG:
  109.                StretchBlt (hdc, 0, 0, cxClient, cyClient, 
  110.                            hdcMem, 0, 0, cxBitmap, cyBitmap, SRCCOPY) ;
  111.                break ;
  112.  
  113.           case IDM_SMALL:
  114.                for (y = 0 ; y < cyClient ; y += cyBitmap)
  115.                for (x = 0 ; x < cxClient ; x += cxBitmap)
  116.                {
  117.                     BitBlt (hdc, x, y, cxBitmap, cyBitmap, 
  118.                             hdcMem, 0, 0, SRCCOPY) ;
  119.                }
  120.                break ;
  121.           }
  122.  
  123.           EndPaint (hwnd, &ps) ;
  124.           return 0 ;
  125.  
  126.      case WM_DESTROY:
  127.           DeleteDC (hdcMem) ;
  128.           DeleteObject (hBitmap) ;
  129.           PostQuitMessage (0) ;
  130.           return 0 ;
  131.      }
  132.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  133. }
  134.