home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / commdlg1.cpp < prev    next >
C/C++ Source or Header  |  1997-07-29  |  3KB  |  141 lines

  1. /*
  2.   Program to test the Open File common dialog box.  The program
  3.   displays the basic statistics for the file you select
  4. */
  5.  
  6. #include <owl\applicat.h>
  7. #include <owl\framewin.h>
  8. #include <owl\opensave.h>
  9. #include "commdlg1.h"
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <dir.h>
  15.  
  16. const MaxStringLen = 256;
  17.  
  18. // declare the custom application class as
  19. // a subclass of TApplication
  20. class TWinApp : public TApplication
  21. {
  22. public:
  23.   TWinApp() : TApplication() {}
  24.  
  25. protected:
  26.   virtual void InitMainWindow();
  27. };
  28.  
  29. // expand the functionality of TWindow by
  30. // deriving class TMainWindow
  31. class TMainWindow : public TWindow
  32. {
  33. public:
  34.  
  35.   TMainWindow();
  36.  
  37. protected:
  38.  
  39.   // the pointer to the data for the File Open dialog box
  40.   TOpenSaveDialog::TData *FileData;
  41.  
  42.   // handle the calculation
  43.   void CMFileStat();
  44.  
  45.   // handle exiting the program
  46.   void CMExit();
  47.  
  48.   // handle closing the window
  49.   virtual BOOL CanClose();
  50.  
  51.   // declare the message map macro
  52.   DECLARE_RESPONSE_TABLE(TMainWindow);
  53.  
  54. };
  55.  
  56. DEFINE_RESPONSE_TABLE1(TMainWindow, TWindow)
  57.   EV_COMMAND(CM_FILESTAT, CMFileStat),
  58.   EV_COMMAND(CM_EXIT, CMExit),
  59. END_RESPONSE_TABLE;
  60.  
  61. TMainWindow::TMainWindow()
  62.   : TWindow(0, 0, 0)
  63. {
  64.   FileData = new TOpenSaveDialog::TData(
  65.        DWORD(OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT),
  66.        "All Files (*.*)|*.*|"
  67.        "C++ Programs (*.cpp)|*.cpp|"
  68.        "Batch files (*.bat)|*.bat||",
  69.        "*.cpp", "", "*.cpp");
  70. }
  71.  
  72. void TMainWindow::CMFileStat()
  73. {
  74.   char selFile[MaxStringLen];
  75.   char s[MaxStringLen];
  76.   char format[MaxStringLen];
  77.   ffblk fileInfo;
  78.   unsigned Hour, Minute, Second, Day, Month, Year,
  79.        uDate, uTime;
  80.   TFileOpenDialog* FileDialog;
  81.  
  82.   FileDialog = new TFileOpenDialog(this, *FileData);
  83.  
  84.   if (FileDialog->Execute() == IDOK) {
  85.     // get the file information
  86.     strcpy(selFile, FileData->FileName);
  87.     findfirst(selFile, &fileInfo, FA_ARCH);
  88.     // build the format string
  89.     strcpy(format, "Filename: %s\n");
  90.      strcat(format, "Time Stamp: %02u:%02u:%02u\n");
  91.      strcat(format, "Date Stamp: %02u/%02u/%u\n");
  92.      strcat(format, "Size: %ld bytes\n");
  93.     uTime = (unsigned)fileInfo.ff_ftime;
  94.     // get the seconds
  95.     Second = 2 * (uTime & 0x1f);
  96.     // get the minutes
  97.     Minute = (uTime >> 5) & 0x3f;
  98.     // get the hours
  99.     Hour = (uTime >> 11) & 0x1f;
  100.     uDate = (unsigned)fileInfo.ff_fdate;
  101.     // get the day
  102.     Day =  uDate & 0x1f;
  103.     // get the month
  104.      Month = (uDate >> 5) & 0xf;
  105.     // get the year
  106.     Year = (uDate >> 9) & 0x7f;
  107.     sprintf(s, format, fileInfo.ff_name, Hour, Minute, Second,
  108.         Month, Day, Year + 1980U, fileInfo.ff_fsize);
  109.      MessageBox(s, "File Statistics",
  110.                     MB_OK | MB_ICONINFORMATION);
  111.   }
  112. }
  113.  
  114. void TMainWindow::CMExit()
  115. {
  116.   Parent->SendMessage(WM_CLOSE);
  117. }
  118.  
  119. BOOL TMainWindow::CanClose()
  120. {
  121.   return MessageBox("Want to close this application?",
  122.             "Query", MB_YESNO | MB_ICONQUESTION) == IDYES;
  123. }
  124.  
  125. void TWinApp::InitMainWindow()
  126. {
  127.   MainWindow = new TFrameWindow(0, "File Statistics",
  128.                 new TMainWindow);
  129.   // load the menu resource
  130.   MainWindow->AssignMenu(TResId(IDM_MAINMENU));
  131.   // enable the keyboard handler
  132.   MainWindow->EnableKBHandler();
  133. }
  134.  
  135. int OwlMain(int /* argc */, char** /*argv[] */)
  136. {
  137.   TWinApp app;
  138.   return app.Run();
  139. }
  140.  
  141.