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

  1. /*---------------------------------------------
  2.    HEAD.C -- Displays beginning (head) of file
  3.              (c) Charles Petzold, 1990
  4.   ---------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #include <direct.h>
  10.  
  11. #define  MAXPATH     100
  12. #define  MAXREAD    2048
  13.  
  14. long FAR PASCAL WndProc  (HWND, WORD, WORD, LONG) ;
  15. long FAR PASCAL ListProc (HWND, WORD, WORD, LONG) ;
  16.  
  17. char    sReadBuffer [MAXREAD] ;
  18. FARPROC lpfnOldList ;
  19.  
  20. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  21.                     LPSTR lpszCmdLine, int nCmdShow)
  22.      {
  23.      static char szAppName [] = "Head" ;
  24.      HWND        hwnd ;
  25.      MSG         msg ;
  26.      WNDCLASS    wndclass ;
  27.  
  28.      if (!hPrevInstance) 
  29.           {
  30.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  31.           wndclass.lpfnWndProc   = WndProc ;
  32.           wndclass.cbClsExtra    = 0 ;
  33.           wndclass.cbWndExtra    = 0 ;
  34.           wndclass.hInstance     = hInstance ;
  35.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  36.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  37.           wndclass.hbrBackground = COLOR_WINDOW + 1 ;
  38.           wndclass.lpszMenuName  = NULL ;
  39.           wndclass.lpszClassName = szAppName ;
  40.  
  41.           RegisterClass (&wndclass) ;
  42.           }
  43.  
  44.      hwnd = CreateWindow (szAppName, "File Head",
  45.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  46.                           CW_USEDEFAULT, CW_USEDEFAULT,
  47.                           CW_USEDEFAULT, CW_USEDEFAULT,
  48.                           NULL, NULL, hInstance, NULL) ;
  49.  
  50.      ShowWindow (hwnd, nCmdShow) ;
  51.      UpdateWindow (hwnd) ;
  52.  
  53.      while (GetMessage (&msg, NULL, 0, 0))
  54.           {
  55.           TranslateMessage (&msg) ;
  56.           DispatchMessage (&msg) ;
  57.           }
  58.      return msg.wParam ;
  59.      }
  60.  
  61. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  62.      {
  63.      static BOOL     bValidFile ;
  64.      static char     szFile [16] ;
  65.      static HWND     hwndList, hwndText ;
  66.      static OFSTRUCT ofs ;
  67.      static RECT     rect ;
  68.      char            szBuffer [MAXPATH + 1] ;
  69.      HDC             hdc ;
  70.      int             iHandle, i, iCount ;
  71.      PAINTSTRUCT     ps ;
  72.      TEXTMETRIC      tm ;
  73.  
  74.      switch (message)
  75.           {
  76.           case WM_CREATE:
  77.                hdc = GetDC (hwnd) ;
  78.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  79.                GetTextMetrics (hdc, &tm) ;
  80.                ReleaseDC (hwnd, hdc) ;
  81.  
  82.                rect.left = 20 * tm.tmAveCharWidth ;
  83.                rect.top  =  3 * tm.tmHeight ;
  84.  
  85.                hwndList = CreateWindow ("listbox", NULL,
  86.                               WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
  87.                               tm.tmAveCharWidth, tm.tmHeight * 3,
  88.                               tm.tmAveCharWidth * 13 +
  89.                                    GetSystemMetrics (SM_CXVSCROLL),
  90.                               tm.tmHeight * 10,
  91.                               hwnd, 1,
  92.                               GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  93.  
  94.                hwndText = CreateWindow ("static", getcwd (szBuffer, MAXPATH),
  95.                               WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  96.                               tm.tmAveCharWidth,           tm.tmHeight,
  97.                               tm.tmAveCharWidth * MAXPATH, tm.tmHeight,
  98.                               hwnd, 2,
  99.                               GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  100.  
  101.                lpfnOldList = (FARPROC) GetWindowLong (hwndList, GWL_WNDPROC) ;
  102.  
  103.                SetWindowLong (hwndList, GWL_WNDPROC,
  104.                          (LONG) MakeProcInstance ((FARPROC) ListProc,
  105.                                    GetWindowWord (hwnd, GWW_HINSTANCE))) ;
  106.  
  107.                SendMessage (hwndList, LB_DIR, 0x37, (LONG) (LPSTR) "*.*") ;
  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 (wParam == 1 && HIWORD (lParam) == LBN_DBLCLK)
  121.                     {
  122.                     if (LB_ERR == (i = (WORD) SendMessage (hwndList,
  123.                                                   LB_GETCURSEL, 0, 0L)))
  124.                          break ;
  125.  
  126.                     SendMessage (hwndList, LB_GETTEXT, i,
  127.                                         (LONG) (char far *) szBuffer) ;
  128.  
  129.                     if (-1 != OpenFile (szBuffer, &ofs, OF_EXIST | OF_READ))
  130.                          {
  131.                          bValidFile = TRUE ;
  132.                          strcpy (szFile, szBuffer) ;
  133.                          getcwd (szBuffer, MAXPATH) ;
  134.                          if (szBuffer [strlen (szBuffer) - 1] != '\\')
  135.                               strcat (szBuffer, "\\") ;
  136.                          SetWindowText (hwndText, strcat (szBuffer, szFile)) ;
  137.                          }
  138.                     else
  139.                          {
  140.                          bValidFile = FALSE ;
  141.                          szBuffer [strlen (szBuffer) - 1] = '\0' ;
  142.                          chdir (szBuffer + 1) ;
  143.                          getcwd (szBuffer, MAXPATH) ;
  144.                          SetWindowText (hwndText, szBuffer) ;
  145.                          SendMessage (hwndList, LB_RESETCONTENT, 0, 0L) ;
  146.                          SendMessage (hwndList, LB_DIR, 0x37,
  147.                                       (LONG) (LPSTR) "*.*") ;
  148.                          }
  149.                     InvalidateRect (hwnd, NULL, TRUE) ;
  150.                     }
  151.                return 0 ;
  152.  
  153.           case WM_PAINT:
  154.                hdc = BeginPaint (hwnd, &ps) ;
  155.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  156.                SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
  157.                SetBkColor   (hdc, GetSysColor (COLOR_WINDOW)) ;
  158.  
  159.                if (bValidFile && -1 != (iHandle =
  160.                          OpenFile (szFile, &ofs, OF_REOPEN | OF_READ)))
  161.                     {
  162.                     i = read (iHandle, sReadBuffer, MAXREAD) ;
  163.                     close (iHandle) ;
  164.                     DrawText (hdc, sReadBuffer, i, &rect, DT_WORDBREAK |
  165.                                    DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX) ;
  166.                     }
  167.                else
  168.                     bValidFile = FALSE ;
  169.  
  170.                EndPaint (hwnd, &ps) ;
  171.                return 0 ;
  172.  
  173.           case WM_DESTROY:
  174.                PostQuitMessage (0) ;
  175.                return 0 ;
  176.           }
  177.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  178.      }
  179.  
  180. long FAR PASCAL ListProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  181.      {
  182.      if (message == WM_KEYDOWN && wParam == VK_RETURN)
  183.  
  184.           SendMessage (GetParent (hwnd), WM_COMMAND, 1,
  185.                          MAKELONG (hwnd, LBN_DBLCLK)) ;
  186.  
  187.      return CallWindowProc (lpfnOldList, hwnd, message, wParam, lParam) ;
  188.      }
  189.