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

  1. /*----------------------------------------------------
  2.    SYSMETS3.C -- System Metrics Display Program No. 3
  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[] = "SysMets3" ;
  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. 3",
  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_PAINT:
  174.                hdc = BeginPaint (hwnd, &ps) ;
  175.  
  176.                nPaintBeg = max (0, nVscrollPos + ps.rcPaint.top / cyChar - 1) ;
  177.                nPaintEnd = min (NUMLINES,
  178.                                 nVscrollPos + ps.rcPaint.bottom / cyChar) ;
  179.  
  180.                for (i = nPaintBeg ; i < nPaintEnd ; i++)
  181.                     {
  182.                     x = cxChar * (1 - nHscrollPos) ;
  183.                     y = cyChar * (1 - nVscrollPos + i) ;
  184.  
  185.                     TextOut (hdc, x, y,
  186.                              sysmetrics[i].szLabel,
  187.                  lstrlen (sysmetrics[i].szLabel)) ;
  188.  
  189.                     TextOut (hdc, x + 18 * cxCaps, y,
  190.                              sysmetrics[i].szDesc,
  191.                  lstrlen (sysmetrics[i].szDesc)) ;
  192.  
  193.                     SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  194.  
  195.                     TextOut (hdc, x + 18 * cxCaps + 40 * cxChar, y,
  196.                              szBuffer,
  197.                              wsprintf (szBuffer, "%5d",
  198.                   GetSystemMetrics (sysmetrics[i].nIndex))) ;
  199.  
  200.                     SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  201.                     }
  202.  
  203.                EndPaint (hwnd, &ps) ;
  204.                return 0 ;
  205.  
  206.           case WM_DESTROY:
  207.                PostQuitMessage (0) ;
  208.                return 0 ;
  209.           }
  210.  
  211.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  212.      }
  213.