home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 70 < prev    next >
Encoding:
Text File  |  1990-11-11  |  8.6 KB  |  250 lines

  1. /*-----------------------------------------------------
  2.    SYSMETS.C -- System Metrics Display Program (Final)
  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[] = "SysMets" ;
  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, "System Metrics",
  36.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  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, nMaxWidth,
  55.                    nVscrollPos, nVscrollMax, nHscrollPos, nHscrollMax ;
  56.      char          szBuffer[10] ;
  57.      HDC           hdc ;
  58.      short         i, x, y, nPaintBeg, nPaintEnd, nVscrollInc, nHscrollInc ;
  59.      PAINTSTRUCT   ps ;
  60.      TEXTMETRIC    tm ;
  61.  
  62.      switch (message)
  63.           {
  64.           case WM_CREATE:
  65.                hdc = GetDC (hwnd) ;
  66.  
  67.                GetTextMetrics (hdc, &tm) ;
  68.                cxChar = tm.tmAveCharWidth ;
  69.                cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  70.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  71.  
  72.                ReleaseDC (hwnd, hdc) ;
  73.  
  74.                nMaxWidth = 40 * cxChar + 18 * cxCaps ;
  75.                return 0 ;
  76.  
  77.           case WM_SIZE:
  78.                cyClient = HIWORD (lParam) ;
  79.                cxClient = LOWORD (lParam) ;
  80.  
  81.            nVscrollMax = max (0, NUMLINES + 2 - cyClient / cyChar) ;
  82.                nVscrollPos = min (nVscrollPos, nVscrollMax) ;
  83.  
  84.                SetScrollRange (hwnd, SB_VERT, 0, nVscrollMax, FALSE) ;
  85.                SetScrollPos   (hwnd, SB_VERT, nVscrollPos, TRUE) ;
  86.  
  87.            nHscrollMax = max (0, 2 + (nMaxWidth - cxClient) / cxChar) ;
  88.                nHscrollPos = min (nHscrollPos, nHscrollMax) ;
  89.  
  90.                SetScrollRange (hwnd, SB_HORZ, 0, nHscrollMax, FALSE) ;
  91.                SetScrollPos   (hwnd, SB_HORZ, nHscrollPos, TRUE) ;
  92.                return 0 ;
  93.  
  94.           case WM_VSCROLL:
  95.                switch (wParam)
  96.                     {
  97.                     case SB_TOP:
  98.                          nVscrollInc = -nVscrollPos ;
  99.                          break ;
  100.  
  101.                     case SB_BOTTOM:
  102.                          nVscrollInc = nVscrollMax - nVscrollPos ;
  103.                          break ;
  104.  
  105.                     case SB_LINEUP:
  106.                          nVscrollInc = -1 ;
  107.                          break ;
  108.  
  109.                     case SB_LINEDOWN:
  110.                          nVscrollInc = 1 ;
  111.                          break ;
  112.  
  113.                     case SB_PAGEUP:
  114.                          nVscrollInc = min (-1, -cyClient / cyChar) ;
  115.                          break ;
  116.  
  117.                     case SB_PAGEDOWN:
  118.                          nVscrollInc = max (1, cyClient / cyChar) ;
  119.                          break ;
  120.  
  121.                     case SB_THUMBTRACK:
  122.                          nVscrollInc = LOWORD (lParam) - nVscrollPos ;
  123.                          break ;
  124.  
  125.                     default:
  126.                          nVscrollInc = 0 ;
  127.                     }
  128.                if (nVscrollInc = max (-nVscrollPos,
  129.                          min (nVscrollInc, nVscrollMax - nVscrollPos)))
  130.                     {
  131.                     nVscrollPos += nVscrollInc ;
  132.                     ScrollWindow (hwnd, 0, -cyChar * nVscrollInc, NULL, NULL) ;
  133.                     SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE) ;
  134.                     UpdateWindow (hwnd) ;
  135.                     }
  136.                return 0 ;
  137.  
  138.           case WM_HSCROLL:
  139.                switch (wParam)
  140.                     {
  141.                     case SB_LINEUP:
  142.                          nHscrollInc = -1 ;
  143.                          break ;
  144.  
  145.                     case SB_LINEDOWN:
  146.                          nHscrollInc = 1 ;
  147.                          break ;
  148.  
  149.                     case SB_PAGEUP:
  150.                          nHscrollInc = -8 ;
  151.                          break ;
  152.  
  153.                     case SB_PAGEDOWN:
  154.                          nHscrollInc = 8 ;
  155.                          break ;
  156.  
  157.                     case SB_THUMBPOSITION:
  158.                          nHscrollInc = LOWORD (lParam) - nHscrollPos ;
  159.                          break ;
  160.  
  161.                     default:
  162.                          nHscrollInc = 0 ;
  163.                     }
  164.                if (nHscrollInc = max (-nHscrollPos,
  165.                          min (nHscrollInc, nHscrollMax - nHscrollPos)))
  166.                     {
  167.                     nHscrollPos += nHscrollInc ;
  168.                     ScrollWindow (hwnd, -cxChar * nHscrollInc, 0, NULL, NULL) ;
  169.                     SetScrollPos (hwnd, SB_HORZ, nHscrollPos, TRUE) ;
  170.                     }
  171.                return 0 ;
  172.  
  173.           case WM_KEYDOWN:
  174.                switch (wParam)
  175.                     {
  176.                     case VK_HOME:
  177.                          SendMessage (hwnd, WM_VSCROLL, SB_TOP, 0L) ;
  178.                          break ;
  179.  
  180.                     case VK_END:
  181.                          SendMessage (hwnd, WM_VSCROLL, SB_BOTTOM, 0L) ;
  182.                          break ;
  183.  
  184.                     case VK_PRIOR:
  185.                          SendMessage (hwnd, WM_VSCROLL, SB_PAGEUP, 0L) ;
  186.                          break ;
  187.  
  188.                     case VK_NEXT:
  189.                          SendMessage (hwnd, WM_VSCROLL, SB_PAGEDOWN, 0L) ;
  190.                          break ;
  191.  
  192.                     case VK_UP:
  193.                          SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0L) ;
  194.                          break ;
  195.  
  196.                     case VK_DOWN:
  197.                          SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0L) ;
  198.                          break ;
  199.  
  200.                     case VK_LEFT:
  201.                          SendMessage (hwnd, WM_HSCROLL, SB_PAGEUP, 0L) ;
  202.                          break ;
  203.  
  204.                     case VK_RIGHT:
  205.                          SendMessage (hwnd, WM_HSCROLL, SB_PAGEDOWN, 0L) ;
  206.                          break ;
  207.                     }
  208.                return 0 ;
  209.  
  210.           case WM_PAINT:
  211.                hdc = BeginPaint (hwnd, &ps) ;
  212.  
  213.                nPaintBeg = max (0, nVscrollPos + ps.rcPaint.top / cyChar - 1) ;
  214.                nPaintEnd = min (NUMLINES,
  215.                                 nVscrollPos + ps.rcPaint.bottom / cyChar) ;
  216.  
  217.                for (i = nPaintBeg ; i < nPaintEnd ; i++)
  218.                     {
  219.                     x = cxChar * (1 - nHscrollPos) ;
  220.                     y = cyChar * (1 - nVscrollPos + i) ;
  221.  
  222.                     TextOut (hdc, x, y,
  223.                              sysmetrics[i].szLabel,
  224.                  lstrlen (sysmetrics[i].szLabel)) ;
  225.  
  226.                     TextOut (hdc, x + 18 * cxCaps, y,
  227.                              sysmetrics[i].szDesc,
  228.                  lstrlen (sysmetrics[i].szDesc)) ;
  229.  
  230.                     SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  231.  
  232.                     TextOut (hdc, x + 18 * cxCaps + 40 * cxChar, y,
  233.                              szBuffer,
  234.                              wsprintf (szBuffer, "%5d",
  235.                   GetSystemMetrics (sysmetrics[i].nIndex))) ;
  236.  
  237.                     SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  238.                     }
  239.  
  240.                EndPaint (hwnd, &ps) ;
  241.                return 0 ;
  242.  
  243.           case WM_DESTROY:
  244.                PostQuitMessage (0) ;
  245.                return 0 ;
  246.           }
  247.  
  248.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  249.      }
  250.