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

  1. /*---------------------------------------------------------
  2.    DEVCAPS1.C -- Device Capabilities Display Program No. 1
  3.                  (c) Charles Petzold, 1998
  4.   ---------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define NUMLINES ((int) (sizeof devcaps / sizeof devcaps [0]))
  9.  
  10. struct
  11. {
  12.      int     iIndex ;
  13.      TCHAR * szLabel ;
  14.      TCHAR * szDesc ;
  15. }
  16. devcaps [] =
  17. {
  18.      HORZSIZE,      TEXT ("HORZSIZE"),     TEXT ("Width in millimeters:"),
  19.      VERTSIZE,      TEXT ("VERTSIZE"),     TEXT ("Height in millimeters:"),
  20.      HORZRES,       TEXT ("HORZRES"),      TEXT ("Width in pixels:"),
  21.      VERTRES,       TEXT ("VERTRES"),      TEXT ("Height in raster lines:"),
  22.      BITSPIXEL,     TEXT ("BITSPIXEL"),    TEXT ("Color bits per pixel:"),
  23.      PLANES,        TEXT ("PLANES"),       TEXT ("Number of color planes:"),
  24.      NUMBRUSHES,    TEXT ("NUMBRUSHES"),   TEXT ("Number of device brushes:"),
  25.      NUMPENS,       TEXT ("NUMPENS"),      TEXT ("Number of device pens:"),
  26.      NUMMARKERS,    TEXT ("NUMMARKERS"),   TEXT ("Number of device markers:"),
  27.      NUMFONTS,      TEXT ("NUMFONTS"),     TEXT ("Number of device fonts:"),
  28.      NUMCOLORS,     TEXT ("NUMCOLORS"),    TEXT ("Number of device colors:"),
  29.      PDEVICESIZE,   TEXT ("PDEVICESIZE"),  TEXT ("Size of device structure:"),
  30.      ASPECTX,       TEXT ("ASPECTX"),      TEXT ("Relative width of pixel:"),
  31.      ASPECTY,       TEXT ("ASPECTY"),      TEXT ("Relative height of pixel:"),
  32.      ASPECTXY,      TEXT ("ASPECTXY"),     TEXT ("Relative diagonal of pixel:"),
  33.      LOGPIXELSX,    TEXT ("LOGPIXELSX"),   TEXT ("Horizontal dots per inch:"),
  34.      LOGPIXELSY,    TEXT ("LOGPIXELSY"),   TEXT ("Vertical dots per inch:"),
  35.      SIZEPALETTE,   TEXT ("SIZEPALETTE"),  TEXT ("Number of palette entries:"),
  36.      NUMRESERVED,   TEXT ("NUMRESERVED"),  TEXT ("Reserved palette entries:"),
  37.      COLORRES,      TEXT ("COLORRES"),     TEXT ("Actual color resolution:")
  38. } ;
  39.  
  40. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  41.  
  42. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  43.                     PSTR szCmdLine, int iCmdShow)
  44. {
  45.      static TCHAR szAppName[] = TEXT ("DevCaps1") ;
  46.      HWND         hwnd ;
  47.      MSG          msg ;
  48.      WNDCLASS     wndclass ;
  49.      
  50.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  51.      wndclass.lpfnWndProc   = WndProc ;
  52.      wndclass.cbClsExtra    = 0 ;
  53.      wndclass.cbWndExtra    = 0 ;
  54.      wndclass.hInstance     = hInstance ;
  55.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  56.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  57.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  58.      wndclass.lpszMenuName  = NULL ;
  59.      wndclass.lpszClassName = szAppName ;
  60.      
  61.      if (!RegisterClass (&wndclass))
  62.      {
  63.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  64.                       szAppName, MB_ICONERROR) ;
  65.           return 0 ;
  66.      }
  67.      
  68.      hwnd = CreateWindow (szAppName, TEXT ("Device Capabilities"),
  69.                           WS_OVERLAPPEDWINDOW,
  70.                           CW_USEDEFAULT, CW_USEDEFAULT,
  71.                           CW_USEDEFAULT, CW_USEDEFAULT,
  72.                           NULL, NULL, hInstance, NULL) ;
  73.      
  74.      ShowWindow (hwnd, iCmdShow) ;
  75.      UpdateWindow (hwnd) ;
  76.      
  77.      while (GetMessage (&msg, NULL, 0, 0))
  78.      {
  79.           TranslateMessage (&msg) ;
  80.           DispatchMessage (&msg) ;
  81.      }
  82.      return msg.wParam ;
  83. }
  84.  
  85. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87.      static int  cxChar, cxCaps, cyChar ;
  88.      TCHAR       szBuffer[10] ;
  89.      HDC         hdc ;
  90.      int         i ;
  91.      PAINTSTRUCT ps ;
  92.      TEXTMETRIC  tm ;
  93.      
  94.      switch (message)
  95.      {
  96.      case WM_CREATE:
  97.           hdc = GetDC (hwnd) ;
  98.           
  99.           GetTextMetrics (hdc, &tm) ;
  100.           cxChar = tm.tmAveCharWidth ;
  101.           cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  102.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  103.           
  104.           ReleaseDC (hwnd, hdc) ;
  105.           return 0 ;
  106.           
  107.      case WM_PAINT:
  108.           hdc = BeginPaint (hwnd, &ps) ;
  109.           
  110.           for (i = 0 ; i < NUMLINES ; i++)
  111.           {
  112.                TextOut (hdc, 0, cyChar * i,
  113.                         devcaps[i].szLabel,
  114.                         lstrlen (devcaps[i].szLabel)) ;
  115.                
  116.                TextOut (hdc, 14 * cxCaps, cyChar * i,
  117.                         devcaps[i].szDesc,
  118.                         lstrlen (devcaps[i].szDesc)) ;
  119.                
  120.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  121.                
  122.                TextOut (hdc, 14 * cxCaps + 35 * cxChar, cyChar * i, szBuffer,
  123.                         wsprintf (szBuffer, TEXT ("%5d"),
  124.                              GetDeviceCaps (hdc, devcaps[i].iIndex))) ;
  125.                
  126.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  127.           }
  128.           
  129.           EndPaint (hwnd, &ps) ;
  130.           return 0 ;
  131.           
  132.      case WM_DESTROY:
  133.           PostQuitMessage (0) ;
  134.           return 0 ;
  135.      }
  136.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  137. }
  138.