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

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