home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 31 < prev    next >
Encoding:
Text File  |  1991-10-23  |  7.2 KB  |  197 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. #ifdef __HIGHC__
  108.                SendMessage (hwndList, LB_DIR, 0x37, (LONG) "*.*") ;
  109. #else
  110.                SendMessage (hwndList, LB_DIR, 0x37, (LONG) (LPSTR) "*.*") ;
  111. #endif
  112.                return 0 ;
  113.  
  114.           case WM_SIZE:
  115.                rect.right  = LOWORD (lParam) ;
  116.                rect.bottom = HIWORD (lParam) ;
  117.                return 0 ;
  118.  
  119.           case WM_SETFOCUS:
  120.                SetFocus (hwndList) ;
  121.                return 0 ;
  122.  
  123.           case WM_COMMAND:
  124.                if (wParam == 1 && HIWORD (lParam) == LBN_DBLCLK)
  125.                     {
  126.                     if (LB_ERR == (i = (WORD) SendMessage (hwndList,
  127.                                                   LB_GETCURSEL, 0, 0L)))
  128.                          break ;
  129.  
  130.                     SendMessage (hwndList, LB_GETTEXT, i,
  131.                                         (LONG) (char far *) szBuffer) ;
  132.  
  133.                     if (-1 != OpenFile (szBuffer, &ofs, OF_EXIST | OF_READ))
  134.                          {
  135.                          bValidFile = TRUE ;
  136.                          strcpy (szFile, szBuffer) ;
  137.                          getcwd (szBuffer, MAXPATH) ;
  138.                          if (szBuffer [strlen (szBuffer) - 1] != '\\')
  139.                               strcat (szBuffer, "\\") ;
  140.                          SetWindowText (hwndText, strcat (szBuffer, szFile)) ;
  141.                          }
  142.                     else
  143.                          {
  144.                          bValidFile = FALSE ;
  145.                          szBuffer [strlen (szBuffer) - 1] = '\0' ;
  146.                          chdir (szBuffer + 1) ;
  147.                          getcwd (szBuffer, MAXPATH) ;
  148.                          SetWindowText (hwndText, szBuffer) ;
  149.                          SendMessage (hwndList, LB_RESETCONTENT, 0, 0L) ;
  150.                          SendMessage (hwndList, LB_DIR, 0x37,
  151. #ifdef __HIGHC__
  152.                                       (LONG) "*.*") ;
  153. #else
  154.                                       (LONG) (LPSTR) "*.*") ;
  155. #endif
  156.                          }
  157.                     InvalidateRect (hwnd, NULL, TRUE) ;
  158.                     }
  159.                return 0 ;
  160.  
  161.           case WM_PAINT:
  162.                hdc = BeginPaint (hwnd, &ps) ;
  163.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  164.                SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
  165.                SetBkColor   (hdc, GetSysColor (COLOR_WINDOW)) ;
  166.  
  167.                if (bValidFile && -1 != (iHandle =
  168.                          OpenFile (szFile, &ofs, OF_REOPEN | OF_READ)))
  169.                     {
  170.                     i = read (iHandle, sReadBuffer, MAXREAD) ;
  171.                     close (iHandle) ;
  172.                     DrawText (hdc, sReadBuffer, i, &rect, DT_WORDBREAK |
  173.                                    DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX) ;
  174.                     }
  175.                else
  176.                     bValidFile = FALSE ;
  177.  
  178.                EndPaint (hwnd, &ps) ;
  179.                return 0 ;
  180.  
  181.           case WM_DESTROY:
  182.                PostQuitMessage (0) ;
  183.                return 0 ;
  184.           }
  185.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  186.      }
  187.  
  188. long FAR PASCAL ListProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  189.      {
  190.      if (message == WM_KEYDOWN && wParam == VK_RETURN)
  191.  
  192.           SendMessage (GetParent (hwnd), WM_COMMAND, 1,
  193.                          MAKELONG (hwnd, LBN_DBLCLK)) ;
  194.  
  195.      return CallWindowProc (lpfnOldList, hwnd, message, wParam, lParam) ;
  196.      }
  197.