Webocreation

Monday, November 23, 2009

Implementing a Modal Dialog Box in an Application

Exercise 4: Implementing a Modal Dialog Box in an Application

Continue with the files you created in Exercise 3, or if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in \Labs\Ch09\Lab01\Ex03.

In this exercise, you will write the code that implements the functionality of the dialog box.

Get values from the edit controls

In this step, you will create two CDlgOpenFiles member functions, GetFile1 and GetFile2, that return the contents of the edit controls.

1. At the bottom of the CDlgOpenFiles class definition in DlgOpenF.h, add an attributes section and declare two public methods.
// attributes
public:
void GetFile1 (CString& rFile);
void GetFile2 (CString& rFile);
2. Save DlgOpenF.h.
3. In DlgOpenF.cpp, implement the methods to set rFile to the corresponding edit control members.
void CDlgOpenFiles::GetFile1(CString& rFile)
{
rFile = m_File1;
}
void CDlgOpenFiles::GetFile2(CString& rFile)
{
rFile = m_File2;
}
4. Save DlgOpenF.cpp.

Validate the files
The simplest way to validate a file name for this application is to test for the existence of the file.
1. Declare the method as protected in the implementation section in DlgOpenF.h.
protected:
BOOL IsValidFileSpec (LPCSTR lpszFileSpec);

2. Save DlgOpenF.h.
3. In DlgOpenF.cpp, check for the existence of the file by calling a static CFile member function.
BOOL CDlgOpenFiles::IsValidFileSpec (LPCSTR lpszFileSpec)
{
CFileStatus status;
return CFile::GetStatus( lpszFileSpec, status);

}
4. Save DlgOpenF.cpp.
Implement OnButtonFile1Browse and OnButtonFile2Browse handlers
The browse buttons invoke the common dialog box after transferring the contents of the corresponding edit box to the File Name field of the common dialog box.
1. Edit the OnButtonFile1Browse handler.
2. Save the control values to the member variables of the dialog box object. The CWnd::UpdateData function initiates the transfer of data between the dialog resource and the member variable. Specifying TRUE as the parameter causes the data to be saved from the resource while a FALSE value will initialize the resource from the dialog class object.
UpdateData(TRUE);
3. Define the filter for the File dialog box.
static char szFilter[] =
"All Files (*.*)|*.*|C++ Files (*.cpp, *.h)|*.cpp;*.h||";
4. Construct the File dialog box as an open dialog box with this filter.
CFileDialog dlg(TRUE,NULL,m_File1,NULL,szFilter);
5. Display the File dialog box as modal.
if (dlg.DoModal() == IDOK)
6. If the user clicked OK, then assign the path back to the members of your dialog box.
{
m_File1 = dlg.GetPathName();
}
7. Copy the data from the dialog box class object to the dialog box.
UpdateData(FALSE);
8. Repeat this procedure for OnButtonFile2Browse. The complete function body follows.
void CDlgOpenFiles::OnButtonFile2Browse()
{
UpdateData(TRUE);
static char szFilter[] =
"All Files (*.*)|*.*|C++ Files (*.cpp, *.h)|*.cpp;*.h||";
CFileDialog dlg(TRUE,NULL,m_File2,NULL,szFilter);
if (dlg.DoModal() == IDOK)
{
m_File2 = dlg.GetPathName();
}
UpdateData(FALSE);
}
9. Save DlgOpenF.cpp.


Respond to the user's clicking OK

1. Edit the code for the OnOK handler.
2. Transfer data from the resource to the member variables of the dialog box.
UpdateData(TRUE);

3. Check to see whether the files are valid using the IsValidFileSpec function. If the files are valid, simply pass control to CDialog's default OnOK handler.
if(IsValidFileSpec(m_File1) &&
(IsValidFileSpec(m_File2)))
{
CDialog::OnOK();
}
4. If either of the file names is not valid, display an error message.
else
{
CString ErrMsg;
AfxFormatString2(ErrMsg, IDS_ERRFMT_INVALIDFILE,
m_File1, m_File2);
AfxMessageBox (ErrMsg);
}
5. Save DlgOpenF.cpp.

The completed code for this exercise is in \Labs\Ch09\Lab01\Ex04.

Exercise 5: Using the New Class in an Application

Continue with the files you created in Exercise 4, or if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in \Labs\Ch09\Lab01\Ex04.

In this exercise you will use the completed CDlgOpenFiles dialog class in the Diff application by modifying the simple CDiffDoc::OnFileOpen, rather than modifying CFileDialog.

Include DlgOpenF.h in DiffDoc.cpp

#include "DlgOpenF.h"

Implement CDlgOpenFiles in OnFileOpen

1. Construct CDlgOpenFiles rather than CFileDialog.
void CDiffDoc::OnFileOpen()
{
CDlgOpenFiles dlg;
2. Show the dialog box as modal.
if(dlg.DoModal() == IDOK)
{
3. If the user clicks OK, get the contents of the two file edit controls through their public interface.
dlg.GetFile1(m_File1);
dlg.GetFile2(m_File2);
4. Call RunComparison to load the files into the splitter windows.
RunComparison(m_File1, m_File2);
}
}
5. Save DiffDoc.cpp. The complete function follows.
void CDiffDoc::OnFileOpen()
{
CDlgOpenFiles dlg;
if(dlg.DoModal() == IDOK)
{
dlg.GetFile1(m_File1);
dlg.GetFile2(m_File2);
RunComparison(m_File1, m_File2);
}
}

Build and run the Diff application

The completed code for this exercise is in \Labs\Ch09\Lab01\Ex05.

2. Click the Compare File static text control to set it as the first in the tab order.
With this control now in the first position, the other numbers adjust accordingly.
3. Click the control that should be second in the tab order — in this case, the Edit control paired with Compare File.
4. Set the remaining controls in the same manner.
5. To end the tab-ordering operation, click inside the Dialog editor window, but outside of the dialog box resource. (You can also press esc to end the session.)

Test the dialog box template again

Save the current file

The completed code for this exercise is in \Labs\Ch09\Lab01\Ex01.

Exercise 2: Creating the Dialog Class and Providing for DDX and DDV

Continue with the files you created in Exercise 1, or if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in \Labs\Ch09\Lab01\Ex01.

This exercise has two parts. In the first, you will use ClassWizard to create a dialog class that is associated with your dialog box template. In the second part, you will add member variables for all controls other than static text controls, and add simple dialog data exchange (DDX) and dialog data validation (DDV) for the edit controls.

Using ClassWizard to Create the Dialog Class

Run the Dialog editor on the IDD_OPENFILES dialog resource

Add a dialog class using ClassWizard

1. In the Dialog editor, be sure that your dialog box template window is the active child window.
2. Invoke ClassWizard.
If the Adding a Class dialog box does not appear automatically, click Add Class.
3. Click Create a new class and then click OK<

No comments:

Post a Comment