SysMets3.c
Upload User: guangxunco
Upload Date: 2007-07-26
Package Size: 2562k
Code Size: 8k
Category:

Windows Develop

Development Platform:

WINDOWS

  1. /*----------------------------------------------------
  2.    SYSMETS3.C -- System Metrics Display Program No. 3
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------------*/
  5. #define WINVER 0x0500
  6. #include <windows.h>
  7. #include "sysmets.h"
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  10.                     PSTR szCmdLine, int iCmdShow)
  11. {
  12.      static TCHAR szAppName[] = TEXT ("SysMets3") ;
  13.      HWND         hwnd ;
  14.      MSG          msg ;
  15.      WNDCLASS     wndclass ;
  16.      
  17.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  18.      wndclass.lpfnWndProc   = WndProc ;
  19.      wndclass.cbClsExtra    = 0 ;
  20.      wndclass.cbWndExtra    = 0 ;
  21.      wndclass.hInstance     = hInstance ;
  22.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  23.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  24.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  25.      wndclass.lpszMenuName  = NULL ;
  26.      wndclass.lpszClassName = szAppName ;
  27.      
  28.      if (!RegisterClass (&wndclass))
  29.      {
  30.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  31.                       szAppName, MB_ICONERROR) ;
  32.           return 0 ;
  33.      }
  34.      
  35.      hwnd = CreateWindow (szAppName, TEXT ("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, iCmdShow) ;
  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. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  52. {
  53.      static int  cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
  54.      HDC         hdc ;
  55.      int         i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
  56.      PAINTSTRUCT ps ;
  57.      SCROLLINFO  si ;
  58.      TCHAR       szBuffer[10] ;
  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.                // Save the width of the three columns
  73.           
  74.           iMaxWidth = 40 * cxChar + 22 * cxCaps ;
  75.           return 0 ;
  76.           
  77.      case WM_SIZE:
  78.           cxClient = LOWORD (lParam) ;
  79.           cyClient = HIWORD (lParam) ;
  80.                // Set vertical scroll bar range and page size
  81.           si.cbSize = sizeof (si) ;
  82.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  83.           si.nMin   = 0 ;
  84.           si.nMax   = NUMLINES - 1 ;
  85.           si.nPage  = cyClient / cyChar ;
  86.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  87.                // Set horizontal scroll bar range and page size
  88.           si.cbSize = sizeof (si) ;
  89.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  90.           si.nMin   = 0 ;
  91.           si.nMax   = 2 + iMaxWidth / cxChar ;
  92.           si.nPage  = cxClient / cxChar ;
  93.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  94.           return 0 ;
  95.           
  96.      case WM_VSCROLL:
  97.                // Get all the vertial scroll bar information
  98.           si.cbSize = sizeof (si) ;
  99.           si.fMask  = SIF_ALL ;
  100.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  101.                // Save the position for comparison later on
  102.           iVertPos = si.nPos ;
  103.           switch (LOWORD (wParam))
  104.           {
  105.           case SB_TOP:
  106.                si.nPos = si.nMin ;
  107.                break ;
  108.                
  109.           case SB_BOTTOM:
  110.                si.nPos = si.nMax ;
  111.                break ;
  112.                
  113.           case SB_LINEUP:
  114.                si.nPos -= 1 ;
  115.                break ;
  116.                
  117.           case SB_LINEDOWN:
  118.                si.nPos += 1 ;
  119.                break ;
  120.                
  121.           case SB_PAGEUP:
  122.                si.nPos -= si.nPage ;
  123.                break ;
  124.                
  125.           case SB_PAGEDOWN:
  126.                si.nPos += si.nPage ;
  127.                break ;
  128.                
  129.           case SB_THUMBTRACK:
  130.                si.nPos = si.nTrackPos ;
  131.                break ;
  132.                
  133.           default:
  134.                break ;         
  135.           }
  136.                // Set the position and then retrieve it.  Due to adjustments
  137.                //   by Windows it may not be the same as the value set.
  138.           si.fMask = SIF_POS ;
  139.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  140.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  141.                // If the position has changed, scroll the window and update it
  142.           if (si.nPos != iVertPos)
  143.           {                    
  144.                ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), 
  145.                                    NULL, NULL) ;
  146.                UpdateWindow (hwnd) ;
  147.           }
  148.           return 0 ;
  149.           
  150.      case WM_HSCROLL:
  151.                // Get all the vertial scroll bar information
  152.           si.cbSize = sizeof (si) ;
  153.           si.fMask  = SIF_ALL ;
  154.                // Save the position for comparison later on
  155.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  156.           iHorzPos = si.nPos ;
  157.           switch (LOWORD (wParam))
  158.           {
  159.           case SB_LINELEFT:
  160.                si.nPos -= 1 ;
  161.                break ;
  162.                
  163.           case SB_LINERIGHT:
  164.                si.nPos += 1 ;
  165.                break ;
  166.                
  167.           case SB_PAGELEFT:
  168.                si.nPos -= si.nPage ;
  169.                break ;
  170.                
  171.           case SB_PAGERIGHT:
  172.                si.nPos += si.nPage ;
  173.                break ;
  174.                
  175.           case SB_THUMBPOSITION:
  176.                si.nPos = si.nTrackPos ;
  177.                break ;
  178.                
  179.           default :
  180.                break ;
  181.           }
  182.                // Set the position and then retrieve it.  Due to adjustments
  183.                //   by Windows it may not be the same as the value set.
  184.           si.fMask = SIF_POS ;
  185.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  186.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  187.           
  188.                // If the position has changed, scroll the window 
  189.           if (si.nPos != iHorzPos)
  190.           {
  191.                ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, 
  192.                              NULL, NULL) ;
  193.           }
  194.           return 0 ;
  195.      case WM_PAINT :
  196.           hdc = BeginPaint (hwnd, &ps) ;
  197.                // Get vertical scroll bar position
  198.           si.cbSize = sizeof (si) ;
  199.           si.fMask  = SIF_POS ;
  200.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  201.           iVertPos = si.nPos ;
  202.                // Get horizontal scroll bar position
  203.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  204.           iHorzPos = si.nPos ;
  205.                // Find painting limits
  206.           iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
  207.           iPaintEnd = min (NUMLINES - 1,
  208.                            iVertPos + ps.rcPaint.bottom / cyChar) ;
  209.           
  210.           for (i = iPaintBeg ; i <= iPaintEnd ; i++)
  211.           {
  212.                x = cxChar * (1 - iHorzPos) ;
  213.                y = cyChar * (i - iVertPos) ;
  214.                
  215.                TextOut (hdc, x, y,
  216.                         sysmetrics[i].szLabel,
  217.                         lstrlen (sysmetrics[i].szLabel)) ;
  218.                
  219.                TextOut (hdc, x + 22 * cxCaps, y,
  220.                         sysmetrics[i].szDesc,
  221.                         lstrlen (sysmetrics[i].szDesc)) ;
  222.                
  223.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  224.                
  225.                TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
  226.                         wsprintf (szBuffer, TEXT ("%5d"),
  227.                              GetSystemMetrics (sysmetrics[i].iIndex))) ;
  228.                
  229.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  230.           }
  231.           EndPaint (hwnd, &ps) ;
  232.           return 0 ;
  233.           
  234.      case WM_DESTROY :
  235.           PostQuitMessage (0) ;
  236.           return 0 ;
  237.      }
  238.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  239. }