11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3. Splitter Window<br />

Sample application 3.1\Sdi\Spw demonstrates how to implement splitter window. It is generated by<br />

Application Wizard, with all settings set to default ones. Four main classes used to implement the<br />

application are CSpwApp, CMainFrame, CSpwDoc and CSpwView.<br />

Each pane of the splitter window must be attached with a view in order to make it work. The view<br />

could be implemented by deriving class from any of the standard view classes: CView, CScrollView,<br />

CRichEditView, CListView, CTreeView etc. In the sample, besides the default view CSpwView created by<br />

the Application Wizard, two other classes are derived from CFormView and CEditView, which will be used<br />

to implement different panes of the splitter window.<br />

Class CFormView is a standard <strong>MFC</strong> class that can be used to create view window from dialog<br />

template. A CFormView class must have a corresponding dialog template resource, which will be used to<br />

create the view. To implement a form view, we must first design dialog template, then derive a new class<br />

from CFormView.<br />

In the sample, the dialog template used to implement the form view is IDD_DIALOG_VIEW. Its styles are<br />

set to “Child” and “No border”, this is exactly the same with that of dialog bar (This is because both splitter<br />

window and dialog bar must be child windows). The dialog template contains a static text control and a<br />

multiple-line edit box. By double clicking on the dialog template resource, we will be prompted to add a<br />

new class for it. In this case the template resource ID will be automatically selected to be used by the new<br />

class. In the sample application, the new class is named CSpwFView. If the dialog template is not open when<br />

adding this new class, we must select the dialog ID by ourselves (Figure 3-2).<br />

Input the new<br />

class name<br />

Select the header and<br />

implementation file name<br />

Select base class<br />

Figure 3-2. Derive class from CFormView<br />

If the dialog template resource<br />

is not open, we must select the<br />

ID manually<br />

The other pane of the splitter window is implemented using edit view. The new class for this window<br />

is derived from CEditView, and its name is CSpwEView.<br />

To split a window, we need to call function CSplitterWnd::CreateStatic(…), which has five<br />

parameters:<br />

BOOL CSplitterWnd::CreateStatic<br />

(<br />

CWnd *pParentWnd, int nRows, int nCols,<br />

DWORD dwStyle=WS_CHILD | WS_VISIBLE,<br />

UINT nID=AFX_IDW_PANE_FIRST<br />

);<br />

The first parameter pParentWnd is a CWnd type pointer that points to the parent window. Because a<br />

splitter window is always the child of frame window, this parameter can not be set to NULL. The second<br />

and third parameters specify the number of rows and columns the splitter window will have. The fourth<br />

parameter dwStyle specifies the styles of splitter window, whose default value is WS_CHILD | WS_VISIBLE.<br />

The fifth parameter, nID, identifies which splitter window is being created. This is necessary because<br />

within one frame window, we can create several nested splitter windows. For the root splitter window (The<br />

splitter window whose parent window is the frame window), this ID must be AFX_IDW_PANE_FIRST. For<br />

other nested splitter windows, this ID need to be obtained from the parent splitter windows by calling<br />

58

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!