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

  1. /*----------------------------------------------------
  2.    SYSMETS1.C -- System Metrics Display Program No. 1
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------------*/
  5.  
  6. #define WINVER 0x0500
  7. #include <windows.h>
  8. #include "sysmets.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("SysMets1") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  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         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.  
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.  
  38.      hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 1"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static int  cxChar, cxCaps, cyChar ;
  58.      HDC         hdc ;
  59.      int         i ;
  60.      PAINTSTRUCT ps ;
  61.      TCHAR       szBuffer [10] ;
  62.      TEXTMETRIC  tm ;
  63.  
  64.      switch (message)
  65.      {
  66.      case WM_CREATE:
  67.           hdc = GetDC (hwnd) ;
  68.  
  69.           GetTextMetrics (hdc, &tm) ;
  70.           cxChar = tm.tmAveCharWidth ;
  71.           cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  72.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  73.  
  74.           ReleaseDC (hwnd, hdc) ;
  75.           return 0 ;
  76.  
  77.      case WM_PAINT :
  78.           hdc = BeginPaint (hwnd, &ps) ;
  79.  
  80.           for (i = 0 ; i < NUMLINES ; i++)
  81.           {
  82.                TextOut (hdc, 0, cyChar * i,                      
  83.                         sysmetrics[i].szLabel,
  84.                         lstrlen (sysmetrics[i].szLabel)) ;
  85.  
  86.                TextOut (hdc, 22 * cxCaps, cyChar * i,      
  87.                         sysmetrics[i].szDesc,
  88.                         lstrlen (sysmetrics[i].szDesc)) ;
  89.  
  90.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  91.  
  92.                TextOut (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,
  93.                         wsprintf (szBuffer, TEXT ("%5d"),
  94.                              GetSystemMetrics (sysmetrics[i].iIndex))) ;
  95.  
  96.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  97.           }
  98.           EndPaint (hwnd, &ps) ;
  99.           return 0 ;
  100.  
  101.      case WM_DESTROY :
  102.           PostQuitMessage (0) ;
  103.           return 0 ;
  104.      }
  105.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  106. }
  107.  
  108.