|
Chapter 4: Implementing View Classes (continued)Creating Static Splitter WindowsA static splitter window is constructed along with the frame window at application startup. The user cannot create or destroy a static splitter window.To create a static splitter window
NOTE: You can use MFC AppWizard or the Component and Controls Gallery to add the splitter object. However, you will need to replace the code generated by MFC AppWizard or the Component and Controls Gallery to make it appropriate for a static splitter window. NOTE: You can use ClassWizard to add new view classes. When you use MFC AppWizard to create an application, you choose the base class for the view in Step 6. MFC AppWizard adds this single view class to your application. You can then use ClassWizard to derive other view classes from appropriate view base classes and display each in a pane of a static splitter window.
To implement a static splitter window in an application
The following sample code shows how to create and implement a static splitter window by using the OnCreateClient function. To copy this code for use in your own projects, see "Implementing a Static Splitter Window" on the accompanying CD-ROM.
// Implementing an application with a static splitter window rc = m_wndSplitter.CreateView(0, 0, Command Handler IssuesIn a dynamic splitter application, command handlers are generally placed in the view class. The view class has the easiest access to the document, and commands often modify the data in the document.However, a static splitter application generally has multiple views. Depending on what is appropriate for your application, you must decide where to place your command handlers. If you want the same commands enabled regardless of which view has focus, place the handlers in the frame class. If you want each view to have its own set of handlers, add the appropriate handlers to each view class.
Coordinating Updates in the ViewsIn applications that use different types of views, calling UpdateAllViews is not the easiest solution for updating data. For information about coordinating multiple views, see "Coordinating Multiple Interrelated Views" on page 183 in this chapter.
Lab 4.1: Adding a Splitter BarIn this lab, you will add a splitter bar to a single document interface (SDI) application.To see the demonstration "Lab 4.1 Demonstration," see the accompanying CD-ROM. Estimated time to complete this lab: 20 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.1\Ex01. The solution code for this lab is located in the folder <install folder>\Labs\Ch04\Lab4.1\Ex01\Solution.
ObjectivesAfter completing this lab, you will be able to:
PrerequisitesThere are no prerequisites for this lab.
ExercisesThe following exercise provides practice working with the concepts and techniques covered in this chapter:
In this exercise, you will add a splitter bar to an SDI window.
Exercise 1: Adding a Splitter WindowThe code that forms the starting point for this exercise is in <install folder>\Labs\Ch04\Lab4.1\Ex01.In this exercise, you will add a splitter bar to an SDI application. Create a new CSplitter class
![]() Figure 4-5.
NOTE: ClassWizard does not present CSplitterWnd as a base class. You can change the base class directly. Change the declaration line of CSplitter from: class CSplitter : public CWnd to: class CSplitter : public CSplitterWnd int GetSplitterWidth() const { return m_cxSplitter; }
BEGIN_MESSAGE_MAP(CSplitter, CWnd) BEGIN_MESSAGE_MAP(CSplitter, CSplitterWnd) Add a reference to the splitter in the MainFrame object
// splitter bar embedded members
Figure 4-6. Add code to the OnCreateClient handler to create the splitter window
m_wndSplitter.CreateStatic (this, 1, 2, WS_CHILD); CSplitterWnd::CreateStatic sets up a constant number and arrangement of splitter panes. SIZE size; m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CDiffView), size, m_wndSplitter.ShowWindow(SW_SHOWNORMAL); 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.1.1 OnCreateClient" on the accompanying CD-ROM.
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, size.cx = (rect.right - m_wndSplitter.GetSplitterWidth())/2;Build, and run the Diff application
#include "splitter.h" #include "splitter.h" The solution code for this exercise is located in the folder <install folder>\Labs\Ch04\Lab4.1\Ex01\Solution.
Implementing Form ViewsThe CFormView class provides a window whose client area contains dialog box controls. These controls support entering, viewing, or altering data that is usually found in a form-based, data-access application.In this section, you will look at creating and implementing form views derived from the CFormView class. This section also provides an overview of the database view classes.
User-Defined Form ViewsTo create a user-defined form view, you must create controls on the form, and handle updating the view and the document. Optionally, you can provide customized printing support.Choosing CFormView as the base class for your application in Step 6 of MFC AppWizard provides a "blank" form, which you can customize with the appropriate controls. The following illustration shows the MFC AppWizard - Step 6 dialog box with CFormView selected.
Figure 4-7. Use the resource editor to open the dialog box resource in the .rc file that is associated with your application view, and to create the controls that you want on the form.
It is important to note that certain properties for the form itself are set by MFC AppWizard and should not be changed. The following table lists the required property settings for a form.
Use ClassWizard to add member variables to the form view class. For example, add CString variables for edit controls, BOOL variables for option buttons, and so on.
TIP: If you have an existing Visual Basic-based application with a form similar to one that you want to use in your form view application, you can import the Visual Basic form into your Visual C++ project. For more information, search for "Using Custom Controls in a Dialog Box" and specifically, "Importing Visual Basic Forms" in MSDN Library Visual Studio 6.0. Override the OnInitialUpdate function of your view class to perform the initialization you want to occur at application startup, such as resizing the frame to fit the view. Override the OnUpdate function of your view class to initialize the controls on the form with data from the document, as required. Add message handlers in the view class to respond to messages generated by the controls contained in the view. Typically, you add code to the message handlers to move data from your view to your document. For example, if your document class contains a string, you would place an edit box on the form. Next, in the OnUpdate function, you would initialize the edit control with the current value of the string. Finally, in the handler for the EN_CHANGE message for each edit control, you would add the code to store the new string value in the appropriate member variable in the document. The sample code on the following page implements a form view with an edit control and three check boxes. To copy this code for use in your own projects, see "Implementing a Form View Application" on the accompanying CD-ROM.
// Samples\CH04\Form void CMyFormView::OnColor(UINT nID) // Determine the state of the color check boxes, build an RGB CFormDoc * pDoc = (CFormDoc *)GetDocument();Optionally, override the OnPrint function in your view class as required. For more information, search for "CFormView" in MSDN Library Visual Studio 6.0.
Database View ClassesThe COleDBRecordView class is derived from CFormView and displays database records in controls. For information about COleDBRecordView, see "COleDBRecordView Class" on page 355 in Chapter 7, "Using OLE DB Templates for Data Access."In addition, MFC provides the following view classes:
Provides database forms for applications that use the MFC ODBC classes. Provides database forms for applications that use the MFC DAO classes.
Microsoft® Mastering: MFC Development Using Microsoft Visual C++® 6.0
Last Updated: Friday, July 6, 2001 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||