Provide an Alternative View to Data Using Splitter Window

<meta content="MSHTML 6.00.2900.3199" name="GENERATOR"> <style></style>2002年06月10日 15:23:00

HOWTO: Provide an Alternative View to Data Using Splitter Window (Q196832)


The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2
    • Microsoft Visual C++, 32-bit Professional Edition, version 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0
    • Microsoft Visual C++, 32-bit Professional Edition, version 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
    • Microsoft Visual C++, 32-bit Professional Edition, version 6.0
    • Microsoft Visual C++, 32-bit Learning Edition, version 6.0



SUMMARY

This article demonstrates how to use a static splitter window in a traditional SDI document-view architecture. The article shows how to design an application that can switch between a single view and a splitter window that can embed as many panes as necessary.


MORE INFORMATION

The solution consists of embedding a pointer to a CSplitterWnd in CMainFrame, as opposed to the recommended way of embedding the CSplitterWnd object. Later, when the application needs to switch from the regular view to the splitter window, this splitter window is created on the heap and initialized by using CreateStatic(), while the regular view is destroyed.

NOTE: To avoid destruction of the CDocument-derived object when the view is destroyed, you need to set its m_bAutoDelete data member to FALSE.

You can easily expand the code to support static splitter windows with different numbers of panes, depending on some run-time conditions.

Steps to Create Sample

  1. Create a new SDI project.

  2. Add a SplitterWnd (ID_VIEW_SPLITTERWND) menu item to a View menu. This item turns the splitter window on and off.

  3. To preserve the document when there is no view associated with it, add add the following code in the CDocument-derived class constructor:

          CMyDoc::CMyDoc()      {         m_bAutoDelete = FALSE;      }
  4. To avoid resource leaks, you also need to set the flag back to true before the destruction of the last view associated with the document (for example, in the CMainFrame handler of WM_CLOSE:

    CMainFrame::OnClose()):
          CMainFrame::OnClose()      {         GetActiveDocument()-<m_bautodelete true cframewnd::onclose><li>Add a pointer to a CSplitterWnd in the CMainFrame definition: <br><br><pre class="kbcode">      class CMainFrame : public CFrameWnd      {      ...        protected:         CSplitterWnd *m_pSplitter;      ...      };</pre>
    </li>
    <li>Initialize this object in the CMainFrame constructor, and delete it in the destructor: <br><br><pre class="kbcode">      CMainFrame::CMainFrame()      {         // TODO: add member initialization code here.         m_pSplitter = NULL;      }      CMainFrame::~CMainFrame()      {         if (m_pSplitter)            delete m_pSplitter;      }</pre>
    </li>
    <li>Add two new CView-derived classes to your project, representing views that are the panes in your CSplitterWnd. <br><br>
    </li>
    <li>Add the following #include statements in your MainFrm.cpp file: <br><br><pre class="kbcode">      #include "MyDoc.h"      #include "MyView.h"      #include "Pane1View.h"      #include "Pane2View.h"</pre>
    </li>
    <li>Use Class Wizard to create a command and an UpdateUI handler in CMainFrame for the menu item you added: <br><br><pre class="kbcode">      void CMainFrame::OnUpdateViewSplitterWnd(CCmdUI* pCmdUI)      {         // TODO: Add your command update UI handler code here.         pCmdUI-<setcheck void cmainframe::onviewsplitterwnd todo: add your command handler code here. ccreatecontext ctx ctx.m_pnewviewclass="RUNTIME_CLASS(CMyView);" getactivedocument call before you destroy m_splitter. ctx.m_pcurrentdoc="GetActiveDocument();" assert m_psplitter when there is a visible splitter. if splitter window. delete create and initialize cmyview. setactiveview getactiveview else new csplitterwnd trace0 return runtime_class csize one of the panes. two panes containing view associated with current document redisplay frame. recalclayout></setcheck></pre>
    </li></m_bautodelete>


Published Dec 8 1998 9:32PM Issue Type kbhowto
Last Modifed Jul 17 2001 4:32PM Additional Query Words
Keywords kbDocView kbMFC KbUIDesign kbVC420 kbVC500 kbVC600 kbGrpDSMFCATL


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=3547


你可能感兴趣的:(C++,c,Microsoft,C#,mfc)