home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 March / Freesoft_1997-03_cd.bin / recenz / 926 / EXMFC.ARJ / EXAMPLES.MFC / EX04DLG.CPP next >
C/C++ Source or Header  |  1996-04-19  |  8KB  |  296 lines

  1. //
  2. // EX04DLG.CPP  - Component of MFC Example Program 04
  3. //
  4. //  C++/DOS Example program for ArchiveLib 2.0
  5. //
  6. //  Copyright (c) Greenleaf Software, Inc. 1996
  7. //  All Rights Reserved
  8. //
  9. // MEMBERS/FUNCTIONS DEMONSTRATED
  10. //
  11. //   ALCreate()
  12. //
  13. // DESCRIPTION
  14. //
  15. //  This example program demonstrates the simplified interface function
  16. //  ALCreate().  It lets you select a list of files from an input list
  17. //  box, then inserts them into MFC00.ZIP on command.  It also demonstrates
  18. //  the use of the callback function for monitoring.
  19. //
  20. // REVISION HISTORY
  21. //
  22. //  March 18, 1996    2.01A : First release
  23. //
  24.  
  25. // ex04dlg.cpp : implementation file
  26. //
  27.  
  28. #include "ex04std.h"
  29. #include "ex04mfc.h"
  30. #include "ex04dlg.h"
  31. #include "stdio.h"
  32.  
  33. #include "alsimple.h"
  34.  
  35. #ifdef _DEBUG
  36. #define new DEBUG_NEW
  37. #undef THIS_FILE
  38. static char THIS_FILE[] = __FILE__;
  39. #endif
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CAboutDlg dialog used for App About
  43.  
  44. class CAboutDlg : public CDialog
  45. {
  46. public:
  47.     CAboutDlg();
  48.  
  49. // Dialog Data
  50.     //{{AFX_DATA(CAboutDlg)
  51.     enum { IDD = IDD_ABOUTBOX };
  52.     //}}AFX_DATA
  53.  
  54.     // ClassWizard generated virtual function overrides
  55.     //{{AFX_VIRTUAL(CAboutDlg)
  56.     protected:
  57.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  58.     //}}AFX_VIRTUAL
  59.  
  60. // Implementation
  61. protected:
  62.     //{{AFX_MSG(CAboutDlg)
  63.     virtual BOOL OnInitDialog();
  64.     //}}AFX_MSG
  65.     DECLARE_MESSAGE_MAP()
  66. };
  67.  
  68. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  69. {
  70.     //{{AFX_DATA_INIT(CAboutDlg)
  71.     //}}AFX_DATA_INIT
  72. }
  73.  
  74. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CDialog::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(CAboutDlg)
  78.     //}}AFX_DATA_MAP
  79. }
  80.  
  81. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  82.     //{{AFX_MSG_MAP(CAboutDlg)
  83.     //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP()
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CEx04mfcDlg dialog
  88.  
  89. CEx04mfcDlg::CEx04mfcDlg(CWnd* pParent /*=NULL*/)
  90.     : CDialog(CEx04mfcDlg::IDD, pParent)
  91. {
  92.     //{{AFX_DATA_INIT(CEx04mfcDlg)
  93.     //}}AFX_DATA_INIT
  94.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  95.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  96. }
  97.  
  98. void CEx04mfcDlg::DoDataExchange(CDataExchange* pDX)
  99. {
  100.     CDialog::DoDataExchange(pDX);
  101.     //{{AFX_DATA_MAP(CEx04mfcDlg)
  102.     //}}AFX_DATA_MAP
  103. }
  104.  
  105. BEGIN_MESSAGE_MAP(CEx04mfcDlg, CDialog)
  106.     //{{AFX_MSG_MAP(CEx04mfcDlg)
  107.     ON_WM_SYSCOMMAND()
  108.     ON_WM_PAINT()
  109.     ON_WM_QUERYDRAGICON()
  110.     ON_BN_CLICKED(IDC_EXIT, OnExit)
  111.     ON_BN_CLICKED(IDC_ABOUT, OnAbout)
  112.     ON_BN_CLICKED(IDC_COMPRESS, OnCompress)
  113.     //}}AFX_MSG_MAP
  114. END_MESSAGE_MAP()
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CEx04mfcDlg message handlers
  118.  
  119. BOOL CEx04mfcDlg::OnInitDialog()
  120. {
  121.     CDialog::OnInitDialog();
  122.  
  123.     // Add "About..." menu item to system menu.
  124.  
  125.     // IDM_ABOUTBOX must be in the system command range.
  126.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  127.     ASSERT(IDM_ABOUTBOX < 0xF000);
  128.  
  129.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  130.     CString strAboutMenu;
  131.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  132.     if (!strAboutMenu.IsEmpty())
  133.     {
  134.         pSysMenu->AppendMenu(MF_SEPARATOR);
  135.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  136.     }
  137.     // Set the icon for this dialog.  The framework does this automatically
  138.     //  when the application's main window is not a dialog
  139.     SetIcon(m_hIcon, TRUE);            // Set big icon
  140.     SetIcon(m_hIcon, FALSE);        // Set small icon
  141.  
  142.     // TODO: Add extra initialization here
  143. //
  144. // These members are all used in the callback monitoring
  145. // function.  I initialize them here so that they will
  146. // be available when I need them.  I also fill the list box
  147. // with the file names from the current directory.
  148. //
  149.     m_FileName = GetDlgItem( IDC_FILE );
  150.     m_FileProgress = GetDlgItem( IDC_PROGRESS2 );
  151.     m_JobProgress = GetDlgItem( IDC_PROGRESS1 );
  152.     m_ListBox = (CListBox *) GetDlgItem( IDC_LIST1 );
  153.     m_ListBox->Dir( 0, "*.*" );
  154.     return TRUE;  // return TRUE  unless you set the focus to a control
  155. }
  156.  
  157. void CEx04mfcDlg::OnSysCommand(UINT nID, LPARAM lParam)
  158. {
  159.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  160.     {
  161.         CAboutDlg dlgAbout;
  162.         dlgAbout.DoModal();
  163.     }
  164.     else
  165.     {
  166.         CDialog::OnSysCommand(nID, lParam);
  167.     }
  168. }
  169.  
  170. // If you add a minimize button to your dialog, you will need the code below
  171. //  to draw the icon.  For MFC applications using the document/view model,
  172. //  this is automatically done for you by the framework.
  173.  
  174. void CEx04mfcDlg::OnPaint() 
  175. {
  176.     if (IsIconic())
  177.     {
  178.         CPaintDC dc(this); // device context for painting
  179.  
  180.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  181.  
  182.         // Center icon in client rectangle
  183.         int cxIcon = GetSystemMetrics(SM_CXICON);
  184.         int cyIcon = GetSystemMetrics(SM_CYICON);
  185.         CRect rect;
  186.         GetClientRect(&rect);
  187.         int x = (rect.Width() - cxIcon + 1) / 2;
  188.         int y = (rect.Height() - cyIcon + 1) / 2;
  189.  
  190.         // Draw the icon
  191.         dc.DrawIcon(x, y, m_hIcon);
  192.     }
  193.     else
  194.     {
  195.         CDialog::OnPaint();
  196.     }
  197. }
  198.  
  199. // The system calls this to obtain the cursor to display while the user drags
  200. //  the minimized window.
  201. HCURSOR CEx04mfcDlg::OnQueryDragIcon()
  202. {
  203.     return (HCURSOR) m_hIcon;
  204. }
  205.  
  206.  
  207. void CEx04mfcDlg::OnExit()
  208. {
  209.     // TODO: Add your control notification handler code here
  210.     PostMessage( WM_QUIT, 0, 0 );
  211. }
  212.  
  213. BOOL CEx04mfcDlg::DestroyWindow() 
  214. {
  215.     // TODO: Add your specialized code here and/or call the base class
  216.     return CDialog::DestroyWindow();
  217. }
  218.  
  219. void CEx04mfcDlg::OnAbout()
  220. {
  221.     // TODO: Add your control notification handler code here
  222.     CAboutDlg dlgAbout;
  223.     dlgAbout.DoModal();
  224. }
  225.  
  226. BOOL CAboutDlg::OnInitDialog() 
  227. {
  228.     CDialog::OnInitDialog();
  229.     
  230.     // TODO: Add extra initialization here
  231.     return TRUE;  // return TRUE unless you set the focus to a control
  232.                   // EXCEPTION: OCX Property Pages should return FALSE
  233. }
  234.  
  235.  
  236. //
  237. // The callback function under the simplified
  238. // interface isn't a class member.  So, in order
  239. // to find the information I need to update the
  240. // user interface, I have to determine what the
  241. // current window object is.  To do that, I have
  242. // to track down the main window of this app by
  243. // looking at the member of theApp that holds it.
  244. // Once I have that, updating the user interface
  245. // elements is a breeze.
  246. //
  247. extern CEx04mfcApp theApp;
  248.  
  249. void AL_EXPORT AL_DLL_FAR my_callback( const char AL_DLL_FAR *name,
  250.                                        int object_ratio,
  251.                                        int job_ratio )
  252. {
  253.     CEx04mfcDlg *w = (CEx04mfcDlg *) theApp.m_pMainWnd;
  254.     if ( name ) {
  255.         w->m_FileName->SetWindowText( name );
  256. //
  257. // This tricky bit turns off the selection status of
  258. // a file when it is opened.
  259. //
  260.         int i = w->m_ListBox->FindString( -1, name );
  261.         if ( i >= 0 ) {
  262.             w->m_ListBox->SetSel( i, 0 );
  263.         }
  264.     }
  265.     if ( object_ratio > 0 )
  266.         w->m_FileProgress->SendMessage( PBM_SETPOS, object_ratio, object_ratio );
  267.     if ( job_ratio > 0 )
  268.         w->m_JobProgress->SendMessage( PBM_SETPOS, job_ratio, job_ratio );
  269. }
  270.  
  271. //
  272. // When the compress button is pressed, I do a couple of things.
  273. // First, I disable the compress button for the duration of the
  274. // app.  Second, I build a long string containing all of the file
  275. // names, and I separate them by commas.  Given that, the call
  276. // to ALCreate() is a breeze.
  277. //
  278. void CEx04mfcDlg::OnCompress()
  279. {
  280.     // TODO: Add your control notification handler code here
  281.     CString s = "";
  282.     for ( int i = 0 ; i < m_ListBox->GetCount() ; i++ ) {
  283.         if ( m_ListBox->GetSel( i ) > 0 ) {
  284.             CString s1;
  285.             if ( s != "" )
  286.                 s += ",";
  287.             m_ListBox->GetText( i, s1 );
  288.             s += s1;
  289.         }
  290.     }
  291.     CButton *b = (CButton *) GetDlgItem( IDC_COMPRESS );
  292.     b->EnableWindow( 0 );
  293.     ALCreate( "mfc00.zip", s, 0, my_callback );
  294.     b->EnableWindow( 1 );
  295. }
  296.