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

  1. /*---------------------------------------------
  2.    HEAD.C -- Displays beginning (head) of file
  3.              (c) Charles Petzold, 1998
  4.   ---------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_LIST     1
  9. #define ID_TEXT     2
  10.  
  11. #define MAXREAD     8192
  12. #define DIRATTR     (DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | \
  13.                      DDL_DIRECTORY | DDL_ARCHIVE  | DDL_DRIVES)
  14. #define DTFLAGS     (DT_WORDBREAK | DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX)
  15.  
  16. LRESULT CALLBACK WndProc  (HWND, UINT, WPARAM, LPARAM) ;
  17. LRESULT CALLBACK ListProc (HWND, UINT, WPARAM, LPARAM) ;
  18.  
  19. WNDPROC OldList ;
  20.  
  21. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  22.                     PSTR szCmdLine, int iCmdShow)
  23. {
  24.      static TCHAR szAppName[] = TEXT ("head") ;
  25.      HWND         hwnd ;
  26.      MSG          msg ;
  27.      WNDCLASS     wndclass ;
  28.      
  29.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  30.      wndclass.lpfnWndProc   = WndProc ;
  31.      wndclass.cbClsExtra    = 0 ;
  32.      wndclass.cbWndExtra    = 0 ;
  33.      wndclass.hInstance     = hInstance ;
  34.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  35.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  36.      wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
  37.      wndclass.lpszMenuName  = NULL ;
  38.      wndclass.lpszClassName = szAppName ;
  39.      
  40.      if (!RegisterClass (&wndclass))
  41.      {
  42.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  43.                       szAppName, MB_ICONERROR) ;
  44.           return 0 ;
  45.      }
  46.      
  47.      hwnd = CreateWindow (szAppName, TEXT ("head"),
  48.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  49.                           CW_USEDEFAULT, CW_USEDEFAULT,
  50.                           CW_USEDEFAULT, CW_USEDEFAULT,
  51.                           NULL, NULL, hInstance, NULL) ;
  52.      
  53.      ShowWindow (hwnd, iCmdShow) ;
  54.      UpdateWindow (hwnd) ;
  55.      
  56.      while (GetMessage (&msg, NULL, 0, 0))
  57.      {
  58.           TranslateMessage (&msg) ;
  59.           DispatchMessage (&msg) ;
  60.      }
  61.      return msg.wParam ;
  62. }
  63.  
  64. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  65. {
  66.      static BOOL     bValidFile ;
  67.      static BYTE     buffer[MAXREAD] ;
  68.      static HWND     hwndList, hwndText ;
  69.      static RECT     rect ;
  70.      static TCHAR    szFile[MAX_PATH + 1] ;
  71.      HANDLE          hFile ;
  72.      HDC             hdc ;
  73.      int             i, cxChar, cyChar ;
  74.      PAINTSTRUCT     ps ;
  75.      TCHAR           szBuffer[MAX_PATH + 1] ;
  76.      
  77.      switch (message)
  78.      {
  79.      case WM_CREATE :
  80.           cxChar = LOWORD (GetDialogBaseUnits ()) ;
  81.           cyChar = HIWORD (GetDialogBaseUnits ()) ;
  82.           
  83.           rect.left = 20 * cxChar ;
  84.           rect.top  =  3 * cyChar ;
  85.           
  86.           hwndList = CreateWindow (TEXT ("listbox"), NULL,
  87.                               WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
  88.                               cxChar, cyChar * 3,
  89.                               cxChar * 13 + GetSystemMetrics (SM_CXVSCROLL),
  90.                               cyChar * 10,
  91.                               hwnd, (HMENU) ID_LIST,
  92.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  93.                               NULL) ;
  94.  
  95.           GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
  96.           
  97.           hwndText = CreateWindow (TEXT ("static"), szBuffer,
  98.                               WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  99.                               cxChar, cyChar, cxChar * MAX_PATH, cyChar,
  100.                               hwnd, (HMENU) ID_TEXT,
  101.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  102.                               NULL) ;
  103.           
  104.           OldList = (WNDPROC) SetWindowLong (hwndList, GWL_WNDPROC,
  105.                                                (LPARAM) ListProc) ;
  106.           
  107.           SendMessage (hwndList, LB_DIR, DIRATTR, (LPARAM) TEXT ("*.*")) ;
  108.           return 0 ;
  109.           
  110.      case WM_SIZE :
  111.           rect.right  = LOWORD (lParam) ;
  112.           rect.bottom = HIWORD (lParam) ;
  113.           return 0 ;
  114.           
  115.      case WM_SETFOCUS :
  116.           SetFocus (hwndList) ;
  117.           return 0 ;
  118.           
  119.      case WM_COMMAND :
  120.           if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_DBLCLK)
  121.           {
  122.                if (LB_ERR == (i = SendMessage (hwndList, LB_GETCURSEL, 0, 0)))
  123.                     break ;
  124.                
  125.                SendMessage (hwndList, LB_GETTEXT, i, (LPARAM) szBuffer) ;
  126.                
  127.                if (INVALID_HANDLE_VALUE != (hFile = CreateFile (szBuffer, 
  128.                          GENERIC_READ, FILE_SHARE_READ, NULL, 
  129.                          OPEN_EXISTING, 0, NULL)))
  130.  
  131.                {
  132.                     CloseHandle (hFile) ;
  133.                     bValidFile = TRUE ;
  134.                     lstrcpy (szFile, szBuffer) ;
  135.                     GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
  136.  
  137.                     if (szBuffer [lstrlen (szBuffer) - 1] != '\\')
  138.                          lstrcat (szBuffer, TEXT ("\\")) ;
  139.                     SetWindowText (hwndText, lstrcat (szBuffer, szFile)) ;
  140.                }
  141.                else
  142.                {
  143.                     bValidFile = FALSE ;
  144.                     szBuffer [lstrlen (szBuffer) - 1] = '\0' ;
  145.  
  146.                          // If setting the directory doesn't work, maybe it's
  147.                          // a drive change, so try that.
  148.  
  149.                     if (!SetCurrentDirectory (szBuffer + 1))
  150.                     {
  151.                          szBuffer [3] = ':' ;
  152.                          szBuffer [4] = '\0' ;
  153.                          SetCurrentDirectory (szBuffer + 2) ;
  154.                     }
  155.  
  156.                          // Get the new directory name and fill the list box.
  157.  
  158.                     GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
  159.                     SetWindowText (hwndText, szBuffer) ;
  160.                     SendMessage (hwndList, LB_RESETCONTENT, 0, 0) ;
  161.                     SendMessage (hwndList, LB_DIR, DIRATTR, 
  162.                                            (LPARAM) TEXT ("*.*")) ;
  163.                }
  164.                InvalidateRect (hwnd, NULL, TRUE) ;
  165.           }
  166.           return 0 ;
  167.           
  168.      case WM_PAINT :
  169.           if (!bValidFile)
  170.                break ;
  171.  
  172.           if (INVALID_HANDLE_VALUE == (hFile = CreateFile (szFile, GENERIC_READ, 
  173.                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
  174.           {
  175.                bValidFile = FALSE ;
  176.                break ;
  177.           }
  178.  
  179.           ReadFile (hFile, buffer, MAXREAD, &i, NULL) ;
  180.           CloseHandle (hFile) ;
  181.  
  182.                // i now equals the number of bytes in buffer.
  183.                // Commence getting a device context for displaying text.
  184.  
  185.           hdc = BeginPaint (hwnd, &ps) ;
  186.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  187.           SetTextColor (hdc, GetSysColor (COLOR_BTNTEXT)) ;
  188.           SetBkColor   (hdc, GetSysColor (COLOR_BTNFACE)) ;
  189.  
  190.                // Assume the file is ASCII
  191.  
  192.           DrawTextA (hdc, buffer, i, &rect, DTFLAGS) ;
  193.  
  194.           EndPaint (hwnd, &ps) ;
  195.           return 0 ;
  196.           
  197.      case WM_DESTROY :
  198.           PostQuitMessage (0) ;
  199.           return 0 ;
  200.      }
  201.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  202. }
  203.      
  204. LRESULT CALLBACK ListProc (HWND hwnd, UINT message, 
  205.                            WPARAM wParam, LPARAM lParam)
  206. {
  207.      if (message == WM_KEYDOWN && wParam == VK_RETURN)
  208.           SendMessage (GetParent (hwnd), WM_COMMAND, 
  209.                        MAKELONG (1, LBN_DBLCLK), (LPARAM) hwnd) ;
  210.           
  211.      return CallWindowProc (OldList, hwnd, message, wParam, lParam) ;
  212. }
  213.