|
Chapter 4: Implementing View Classes (continued)Creating an Explorer-Style ApplicationMany Windows system applications use the Explorer user interface, in which the client area is split vertically. The left side of the client area shows an overview pane, which makes navigation and browsing possible, while the right side of the client area shows details about the selection in the left pane.You can create an Explorer-Style application by using MFC AppWizard and selecting the Windows Explorer option in Step 5. The following illustration shows the MFC AppWizard - Step 5 dialog box.
Figure 4-15. MFC AppWizard generates code so that each frame window that the application creates is split vertically using the CSplitterWnd object. The left pane always contains a tree view control. The right pane contains a list view control by default. You can modify the view type of the right pane to customize the display detail of the selected item in the left pane. The following illustration shows the MFC AppWizard - Step 6 dialog box with a list of view types.
Figure 4-16. If you use the MFC AppWizard default list view in the right pane, the wizard creates additional menu choices and toolbar buttons to switch the view’s style among report mode, small icon mode, and list mode.
Coordinating Multiple Interrelated ViewsIf your application uses two or more views of different types, you need to ensure that the display of data in the views is always synchronized.There are two methods for coordinating the data in the views. The first method uses the UpdateAllViews function to update the views, whereas the second method updates the views without using this function. Which method you use depends on the requirements of your application.
Coordinating Data Using the UpdateAllViews FunctionUsing UpdateAllViews takes advantage of the document/view architecture as provided by MFC. Suppose that the user does something in a list view that causes a change in a form view. The list view packages the information it needs to pass to the form in a class derived from CObject. The list then calls UpdateAllViews, passing three parameters: a pointer to itself, NULL (0), and a pointer to the object where information is packaged.The document object then calls the form’s OnUpdate function, and passes the second and third arguments of UpdateAllViews to the form. The document does not call the list’s OnUpdate function, because the list specified a pointer to itself in the first argument to UpdateAllViews.
Coordinating Data Without Using the UpdateAllViews FunctionThe second method requires you to manually control the process of coordinating the data. With this method, the document stores pointers to the two view objects. Each view initializes this pointer to itself in its OnCreate function. Each view also has as many public functions, with as many parameters as are needed, to respond to changes requested by the other view. When, for instance, the list needs to cause the form to change its display, the list obtains the pointer to the form from the document and calls a specific function in the form, passing arguments as required. Using this method may be much easier than packaging information in a class and passing that class by using the UpdateAllViews function as described in the first method.
Lab 4.2: Adding Open File Dialogs and a Rich Edit ViewIn this lab, you will use a Common Open File dialog box and a rich edit view in an application.To see the demonstration "Lab 4.2 Demonstration," see the accompanying CD-ROM. Estimated time to complete this lab: 60 minutes To complete the exercises in this lab, you must have the required software. For detailed information about the labs and setup for the labs, see "Labs" in "About This Course." The code that forms the starting point for this lab is located in the folder <install folder>\Labs\Ch04\Lab4.2. The solution code for this lab is located in the folder <install folder>\Labs\Ch04\Lab4.2.
ObjectivesAfter completing this lab, you will be able to:
PrerequisitesThere are no prerequisites for this lab.
ExercisesThe following exercises provide practice working with the concepts and techniques covered in this chapter:
In this exercise, you will use the text editors of Visual C++ to modify the Document and View classes to inherit from their respective rich edit classes. In this exercise, you will add a handler for the Open File dialog box in the ShowDiff application. In this exercise, you will use the rich edit views to display the file that the user chooses in the File dialog box. You will create a function that serializes the text data into the rich edit views, and drive that function from the OnFileOpen handler. In this exercise, you will modify the behavior of CRichEdit so that it will open a file that is dragged from Windows Explorer to the window pane of the running application.
Exercise 1: Modifying the Base Classes of Views and DocumentsThe code that forms the starting point for this exercise is in <install folder>\Labs\Ch04\Lab4.2\Ex01.In this exercise, you will use the text editors of Visual C++ to modify the Document and View classes to inherit from their respective rich edit classes. Replace CDocument with CRichEditDoc
Replace CView with CRichEditView
Add the #includes that support rich edit classes to the file Stdafx.h
#include <afxole.h> #include <afxrich.h> Add the support for CRichEditCntrItem Derived classes from CRichEditDoc need to provide a pointer to a control item (the class that contains the rich edit control itself). This simple function is implemented in DiffDoc.h.
virtual CRichEditCntrItem* CreateClientItem(REOBJECT* preo) const; CRichEditCntrItem* CDiffDoc::CreateClientItem(REOBJECT* preo) const The solution code for this exercise is located in the folder <install folder>\Labs\Ch04\Lab4.2\Ex01\Solution.
Exercise 2: Handling the Common Open File Dialog BoxContinue 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 <install folder>\Labs\Ch04\Lab4.2\Ex02.In this exercise, you will add a handler for the Open File dialog box to the ShowDiff application. Add an OnFileOpen handler for CDiffDoc
Figure 4-17.
Declare protected file name members
CString m_File1 Complete the OnFileOpen handler
static char BASED_CODE szFilter[] = CFileDialog dlg(TRUE,NULL,NULL,NULL,szFilter); The following sample code shows an example of how your code should look. To copy this code for use in your own projects, see "Lab 4.2.2 OnFileOpen" on the accompanying CD-ROM.
void CDiffDoc::OnFileOpen()The solution code for this exercise is located in the folder <install folder>\Labs\Ch04\Lab4.2\Ex02\Solution.
Exercise 3: Using the Rich Edit Views in the ApplicationContinue with the files you created in Exercise 2 or, if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in <install folder>\Labs\Ch04\Lab4.2\Ex03.In this exercise, you will use the rich edit views to display the file that the user chooses in the File dialog box. You will create a function that serializes text data into the rich edit views, and drive that function from the OnFileOpen handler. Get the application and the main frame
class CMainFrame; public: CMainFrame * GetMainFrame() Note that the purpose of steps 3 and 4 is to simplify writing the code for the rest of this lab. Initialize the file members and set the views to text only
m_File1 = _T("");
m_bRTF = FALSE; Add the function to display the files in the views
void RunComparison(LPCSTR lpszFile1, LPCSTR lpszFile2) #include "diffdoc.h" Write the code for the Run Comparison function
CMainFrame * pFrame = CDiffApp::GetApp()->GetMainFrame(); CDiffView * pView; CFile file(lpszFile1, CFile::modeRead); SetModifiedFlag(FALSE); The following sample code shows an example of how your code should look. To copy this code for use in your own projects, see "Lab 4.2.3 RunComparison" on the accompanying CD-ROM.
void CDiffDoc::RunComparison (LPCSTR lpszFile1,Call RunComparison from OnFileOpen
RunComparison (m_File1, m_File2); Set the rich edit control to read-only
GetRichEditCtrl().SetReadOnly(); Make the changes to CMainFrame
CSplitter * GetSplitter() { return &m_wndSplitter; }
//SetActiveView((CView *)m_wndSplitter.GetPane(0,1)); Build and test the application
The solution code for this exercise is located in the folder <install folder>\Labs\Ch04\Lab4.2\Ex03\Solution.
Exercise 4: Managing Drag-and-Drop EditingContinue 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 <install folder>\Labs\Ch04\Lab4.2\Ex04.In this exercise, you will modify the behavior of CRichEdit so that it will open a file that is dragged from Windows Explorer to the window pane of the running application. For purposes of this exercise, you will process dropped files only if two files are dropped. Ignore any additional files beyond the first two. Implement CMain Frame::OnDropFiles
UINT nFileCount = if (nFileCount >= 2) Edit CMainFrame::OnDropFiles to get the first two dropped files and pass them to CDiffDoc::RunComparison
CString File1; ((CDiffDoc *)GetActiveDocument())-> ::DragFinish(hDropInfo); The following sample code shows an example of how your code should look. To copy this code for use in your own projects, see "Lab 4.2.4 CMainFrame::OnDropFiles" on the accompanying CD-ROM.
void CMainFrame::OnDropFiles(HDROP hDropInfo)Add a handler for WM_DROPFILES to CDiffView
Pass the WM_DROPFILES message to CMainFrame from CDiffView
CDiffApp::GetApp()->GetMainFrame() CDiffApp::GetApp()->GetMainFrame()->SendMessage( WM_DROPFILES, //CrichEditView::OnDropFiles(hDropInfo); The following sample code shows an example of how your code should look. To copy this code for use in your own projects, see "Lab 4.2.4 CDiffView::OnDropFiles" on the accompanying CD-ROM. void CDiffView::OnDropFiles(HDROP hDropInfo) #include "splitter.h" Build and test the application
You will see that the files appear in the two panes of the window. The solution code for this exercise is located in the folder <install folder>\Labs\Ch04\Lab4.2\Ex04\Solution.
Microsoft® Mastering: MFC Development Using Microsoft Visual C++® 6.0
Last Updated: Friday, July 6, 2001 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||