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

  1. /*----------------------------------------------------
  2.    SYSMETS2.C -- System Metrics Display Program No. 2
  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[] = "SysMets2" ;
  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. 2",
  36.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  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, cxClient, cyClient, nVscrollPos ;
  55.      char         szBuffer[10] ;
  56.      HDC          hdc ;
  57.      short        i, y ;
  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.  
  73.                SetScrollRange (hwnd, SB_VERT, 0, NUMLINES, FALSE) ;
  74.                SetScrollPos   (hwnd, SB_VERT, nVscrollPos, TRUE) ;
  75.                return 0 ;
  76.  
  77.           case WM_SIZE:
  78.                cyClient = HIWORD (lParam) ;
  79.                cxClient = LOWORD (lParam) ;
  80.                return 0 ;
  81.  
  82.           case WM_VSCROLL:
  83.                switch (wParam)
  84.                     {
  85.                     case SB_LINEUP:
  86.                          nVscrollPos -= 1 ;
  87.                          break ;
  88.  
  89.                     case SB_LINEDOWN:
  90.                          nVscrollPos += 1 ;
  91.                          break ;
  92.  
  93.                     case SB_PAGEUP:
  94.                          nVscrollPos -= cyClient / cyChar ;
  95.                          break ;
  96.  
  97.                     case SB_PAGEDOWN:
  98.                          nVscrollPos += cyClient / cyChar ;
  99.                          break ;
  100.  
  101.                     case SB_THUMBPOSITION:
  102.                          nVscrollPos = LOWORD (lParam) ;
  103.                          break ;
  104.  
  105.                     default:
  106.                          break ;
  107.                     }
  108.                nVscrollPos = max (0, min (nVscrollPos, NUMLINES)) ;
  109.  
  110.                if (nVscrollPos != GetScrollPos (hwnd, SB_VERT))
  111.                     {
  112.                     SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE) ;
  113.                     InvalidateRect (hwnd, NULL, TRUE) ;
  114.                     }
  115.                return 0 ;
  116.  
  117.           case WM_PAINT:
  118.                hdc = BeginPaint (hwnd, &ps) ;
  119.  
  120.                for (i = 0 ; i < NUMLINES ; i++)
  121.                     {
  122.                     y = cyChar * (1 - nVscrollPos + i) ;
  123.  
  124.                     TextOut (hdc, cxChar, y,
  125.                              sysmetrics[i].szLabel,
  126.                  lstrlen (sysmetrics[i].szLabel)) ;
  127.  
  128.                     TextOut (hdc, cxChar + 18 * cxCaps, y,
  129.                              sysmetrics[i].szDesc,
  130.                  lstrlen (sysmetrics[i].szDesc)) ;
  131.  
  132.                     SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  133.  
  134.                     TextOut (hdc, cxChar + 18 * cxCaps + 40 * cxChar, y,
  135.                              szBuffer,
  136.                              wsprintf (szBuffer, "%5d",
  137.                   GetSystemMetrics (sysmetrics[i].nIndex))) ;
  138.  
  139.                     SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  140.                     }
  141.  
  142.                EndPaint (hwnd, &ps) ;
  143.                return 0 ;
  144.  
  145.           case WM_DESTROY:
  146.                PostQuitMessage (0) ;
  147.                return 0 ;
  148.           }
  149.  
  150.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  151.      }
  152.