How to provide an alternative view of data by using a splitter window

來源:互聯網
上載者:User

好幾次都想隨心所欲的控制表單的分割,這次總算搞明白了,原來這個很搞不明白,因為不知道m_bAutoDelete這個變數是doc內部自有的,所以自己定義了一個變數,結果編譯時間老出錯,還以為微軟有問題呢,結果還是自己搞錯了。現在把原文貼出來:

How to provide an alternative view of data by using a splitter windowThis article was previously published under Q196832

SUMMARYThis 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 INFORMATIONThe 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();      }

5. Add a pointer to a CSplitterWnd in the CMainFrame definition:

      class CMainFrame : public CFrameWnd      {      ...        protected:         CSplitterWnd *m_pSplitter;      ...      };

6. Initialize this object in the CMainFrame constructor, and delete it in the destructor:

      CMainFrame::CMainFrame()      {         // TODO: add member initialization code here.         m_pSplitter = NULL;      }      CMainFrame::~CMainFrame()      {         if (m_pSplitter)            delete m_pSplitter;      }

7. Add two new CView-derived classes to your project, representing views that are the panes in your CSplitterWnd.
8. Add the following #include statements in your MainFrm.cpp file:

      #include "MyDoc.h"      #include "MyView.h"      #include "Pane1View.h"      #include "Pane2View.h"

9. Use Class Wizard to create a command and an UpdateUI handler in CMainFrame for the menu item you added:

      void CMainFrame::OnUpdateViewSplitterWnd(CCmdUI* pCmdUI)      {         // TODO: Add your command update UI handler code here.         pCmdUI->SetCheck(m_pSplitter!=NULL);      }      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(ctx.m_pCurrentDoc!=NULL);         // m_pSplitter !=NULL when there is a visible splitter.         if (m_pSplitter)         {            // Destroy splitter window.            delete m_pSplitter;            m_pSplitter = NULL;            // Create and initialize CMyView.            SetActiveView((CView*)CreateView(&ctx));            GetActiveView()->OnInitialUpdate();         }         else         {            // Destroy CMyView.            GetActiveView()->DestroyWindow();            // Create new splitter window.            m_pSplitter = new CSplitterWnd;            if (!m_pSplitter->CreateStatic(this, 1, 2))            {               TRACE0("Can't create splitter window./n");               return;            }            else            {               if ((!m_pSplitter->CreateView(0, 0,                                             RUNTIME_CLASS(CPane1View),                                             CSize(200, 0), &ctx))                 ||(!m_pSplitter->CreateView(0, 1,                                             RUNTIME_CLASS(CPane2View),                                             CSize(500, 0), &ctx)))               {                  TRACE0("Can't create one of the splitter panes./n");                  return;               }               // Initialize the two panes (each containing a view               // associated with the current document).               ((CView*)m_pSplitter->GetPane(0, 0))->OnInitialUpdate();               ((CView*)m_pSplitter->GetPane(0, 1))->OnInitialUpdate();               SetActiveView((CView*)m_pSplitter->GetPane(0, 0));            }         }         // Redisplay frame.         RecalcLayout();      }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.