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

  1. /*----------------------------------------------------
  2.    SYSMETS4.C -- System Metrics Display Program No. 4
  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 ("SysMets4") ;
  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 ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 4"),
  39.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  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, cxClient, cyClient, iMaxWidth ;
  58.      HDC         hdc ;
  59.      int         i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
  60.      PAINTSTRUCT ps ;
  61.      SCROLLINFO  si ;
  62.      TCHAR       szBuffer[10] ;
  63.      TEXTMETRIC  tm ;
  64.      
  65.      switch (message)
  66.      {
  67.      case WM_CREATE:
  68.           hdc = GetDC (hwnd) ;
  69.           
  70.           GetTextMetrics (hdc, &tm) ;
  71.           cxChar = tm.tmAveCharWidth ;
  72.           cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  73.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  74.           
  75.           ReleaseDC (hwnd, hdc) ;
  76.  
  77.                // Save the width of the three columns
  78.           
  79.           iMaxWidth = 40 * cxChar + 22 * cxCaps ;
  80.           return 0 ;
  81.           
  82.      case WM_SIZE:
  83.           cxClient = LOWORD (lParam) ;
  84.           cyClient = HIWORD (lParam) ;
  85.  
  86.                // Set vertical scroll bar range and page size
  87.  
  88.           si.cbSize = sizeof (si) ;
  89.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  90.           si.nMin   = 0 ;
  91.           si.nMax   = NUMLINES - 1 ;
  92.           si.nPage  = cyClient / cyChar ;
  93.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  94.  
  95.                // Set horizontal scroll bar range and page size
  96.  
  97.           si.cbSize = sizeof (si) ;
  98.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  99.           si.nMin   = 0 ;
  100.           si.nMax   = 2 + iMaxWidth / cxChar ;
  101.           si.nPage  = cxClient / cxChar ;
  102.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  103.           return 0 ;
  104.           
  105.      case WM_VSCROLL:
  106.                // Get all the vertical scroll bar information
  107.  
  108.           si.cbSize = sizeof (si) ;
  109.           si.fMask  = SIF_ALL ;
  110.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  111.  
  112.                // Save the position for comparison later on
  113.  
  114.           iVertPos = si.nPos ;
  115.  
  116.           switch (LOWORD (wParam))
  117.           {
  118.           case SB_TOP:
  119.                si.nPos = si.nMin ;
  120.                break ;
  121.                
  122.           case SB_BOTTOM:
  123.                si.nPos = si.nMax ;
  124.                break ;
  125.                
  126.           case SB_LINEUP:
  127.                si.nPos -= 1 ;
  128.                break ;
  129.                
  130.           case SB_LINEDOWN:
  131.                si.nPos += 1 ;
  132.                break ;
  133.                
  134.           case SB_PAGEUP:
  135.                si.nPos -= si.nPage ;
  136.                break ;
  137.                
  138.           case SB_PAGEDOWN:
  139.                si.nPos += si.nPage ;
  140.                break ;
  141.                
  142.           case SB_THUMBTRACK:
  143.                si.nPos = si.nTrackPos ;
  144.                break ;
  145.                
  146.           default:
  147.                break ;         
  148.           }
  149.                // Set the position and then retrieve it.  Due to adjustments
  150.                //   by Windows it might not be the same as the value set.
  151.  
  152.           si.fMask = SIF_POS ;
  153.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  154.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  155.  
  156.                // If the position has changed, scroll the window and update it
  157.  
  158.           if (si.nPos != iVertPos)
  159.           {                    
  160.                ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), 
  161.                                    NULL, NULL) ;
  162.                UpdateWindow (hwnd) ;
  163.           }
  164.           return 0 ;
  165.           
  166.      case WM_HSCROLL:
  167.                // Get all the vertical scroll bar information
  168.  
  169.           si.cbSize = sizeof (si) ;
  170.           si.fMask  = SIF_ALL ;
  171.  
  172.                // Save the position for comparison later on
  173.  
  174.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  175.           iHorzPos = si.nPos ;
  176.  
  177.           switch (LOWORD (wParam))
  178.           {
  179.           case SB_LINELEFT:
  180.                si.nPos -= 1 ;
  181.                break ;
  182.                
  183.           case SB_LINERIGHT:
  184.                si.nPos += 1 ;
  185.                break ;
  186.                
  187.           case SB_PAGELEFT:
  188.                si.nPos -= si.nPage ;
  189.                break ;
  190.                
  191.           case SB_PAGERIGHT:
  192.                si.nPos += si.nPage ;
  193.                break ;
  194.                
  195.           case SB_THUMBPOSITION:
  196.                si.nPos = si.nTrackPos ;
  197.                break ;
  198.                
  199.           default:
  200.                break ;
  201.           }
  202.                // Set the position and then retrieve it.  Due to adjustments
  203.                //   by Windows it might not be the same as the value set.
  204.  
  205.           si.fMask = SIF_POS ;
  206.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  207.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  208.           
  209.                // If the position has changed, scroll the window 
  210.  
  211.           if (si.nPos != iHorzPos)
  212.           {
  213.                ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, 
  214.                              NULL, NULL) ;
  215.           }
  216.           return 0 ;
  217.  
  218.      case WM_KEYDOWN:
  219.           switch (wParam)
  220.           {
  221.           case VK_HOME:
  222.                SendMessage (hwnd, WM_VSCROLL, SB_TOP, 0) ;
  223.                break ;
  224.                
  225.           case VK_END:
  226.                SendMessage (hwnd, WM_VSCROLL, SB_BOTTOM, 0) ;
  227.                break ;
  228.                
  229.           case VK_PRIOR:
  230.                SendMessage (hwnd, WM_VSCROLL, SB_PAGEUP, 0) ;
  231.                break ;
  232.                
  233.           case VK_NEXT:
  234.                SendMessage (hwnd, WM_VSCROLL, SB_PAGEDOWN, 0) ;
  235.                break ;
  236.                
  237.           case VK_UP:
  238.                SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0) ;
  239.                break ;
  240.                
  241.           case VK_DOWN:
  242.                SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0) ;
  243.                break ;
  244.                
  245.           case VK_LEFT:
  246.                SendMessage (hwnd, WM_HSCROLL, SB_PAGEUP, 0) ;
  247.                break ;
  248.                
  249.           case VK_RIGHT:
  250.                SendMessage (hwnd, WM_HSCROLL, SB_PAGEDOWN, 0) ;
  251.                break ;
  252.           }
  253.           return 0 ;
  254.  
  255.      case WM_PAINT:
  256.           hdc = BeginPaint (hwnd, &ps) ;
  257.  
  258.                // Get vertical scroll bar position
  259.  
  260.           si.cbSize = sizeof (si) ;
  261.           si.fMask  = SIF_POS ;
  262.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  263.           iVertPos = si.nPos ;
  264.  
  265.                // Get horizontal scroll bar position
  266.  
  267.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  268.           iHorzPos = si.nPos ;
  269.  
  270.                // Find painting limits
  271.  
  272.           iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
  273.           iPaintEnd = min (NUMLINES - 1,
  274.                            iVertPos + ps.rcPaint.bottom / cyChar) ;
  275.           
  276.           for (i = iPaintBeg ; i <= iPaintEnd ; i++)
  277.           {
  278.                x = cxChar * (1 - iHorzPos) ;
  279.                y = cyChar * (i - iVertPos) ;
  280.                
  281.                TextOut (hdc, x, y,
  282.                         sysmetrics[i].szLabel,
  283.                         lstrlen (sysmetrics[i].szLabel)) ;
  284.                
  285.                TextOut (hdc, x + 22 * cxCaps, y,
  286.                         sysmetrics[i].szDesc,
  287.                         lstrlen (sysmetrics[i].szDesc)) ;
  288.                
  289.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  290.                
  291.                TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
  292.                         wsprintf (szBuffer, TEXT ("%5d"),
  293.                              GetSystemMetrics (sysmetrics[i].iIndex))) ;
  294.                
  295.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  296.           }
  297.  
  298.           EndPaint (hwnd, &ps) ;
  299.           return 0 ;
  300.           
  301.      case WM_DESTROY:
  302.           PostQuitMessage (0) ;
  303.           return 0 ;
  304.      }
  305.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  306. }
  307.