Provide an Alternative View to Data Using Splitter Window

原创 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();
          }
  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();
         
     }
    


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

另类数据Alternative Data行业调查

Alternative Data行业调查 1. 另类数据的定义: “Alternative data in finance refers to any data that is not tradi...
  • Richard_More
  • Richard_More
  • 2016年11月15日 00:08
  • 813

用Win32实现带分割条(Splitter)的窗口

在MFC中借助向导的帮助很容易就能创建一个带有分隔条(Splitter)的窗口,但是在Win32中一切都没有那么容易,在这篇文章里我带领大家用Win32实现一个带这种Splitter的窗口。 一个S...
  • dong_mingyi
  • dong_mingyi
  • 2015年02月06日 18:23
  • 1302

安装 APC(Alternative PHP Cache) 提升 PHP 的解析速度

本文引用《高性能 PHP 开发》来做下记录 无论从命令行还是从 Web 服务器上执行 PHP 脚本时,PHP 必须执行所需的5个步骤。 Zend 引擎必须从文件系统中读取该文件、扫描其词典和表达式、解...
  • liruxing1715
  • liruxing1715
  • 2015年01月07日 16:44
  • 2145

linux下查看负载信息

linux与win管理内存不一样,前者是充分利用系统内存,后者无论使用多大内存都一定使用虚拟内存(linux中swap)。 所以linux内存使用达到90%是正常,但是不会达到100%,因为达到时就...
  • pizicai105
  • pizicai105
  • 2013年05月08日 16:09
  • 2136

【Qt】之 Splitter分割窗口

中英文对照Splitter handles(分隔条)  一个QSplitter控件中可以包含其他控件,这些控件被一个分隔条隔开,托拽这个分隔条,里面的控件的大小可以改变。QSplitter控件经...
  • imxiangzi
  • imxiangzi
  • 2016年09月13日 15:42
  • 1063

MFC视图切换大全总结

纯真视图之间的切换   单文档多视图切换是我在进修MFC中碰到的一个垂老难题目,在今天总算是一一了。我感觉视图切换分为三个等级,第一是在未切分窗格的景象下切换视图类;第二是在分别窗格的一个窗...
  • yongan1006
  • yongan1006
  • 2016年11月09日 14:37
  • 585

特大喜讯,View and Data API 现在支持中文界面了

大家经常会问到,使用View and Data API怎么做界面的本地化,来显示中文,现在好消息来了,从v1.2.19起,View and Data API开始支持多国语言界面了。你需要制定版本号为v...
  • u012141979
  • u012141979
  • 2015年12月23日 16:35
  • 1296

理清Activity、View及Window之间关系

View、Window以及Activity主要是用于显示并与用户交互的。这让我们在初学的时候很容易弄混,而且无法理解他们区别以及联系。本文是笔者查阅相关资料后,结合自己的理解写出来。希望能帮你梳理清楚...
  • huachao1001
  • huachao1001
  • 2016年07月26日 11:33
  • 8279

WinForm 编程中控件的使用小技巧(splitter实现窗口分区域,改变Button的形状)

一、Splitter 控件的主要作用就是将窗口分区并可以调节区域大小,比如:我想将窗口分为左右两个区域,并可以调节两个区域所占的比例。操作流程如下: 1、新建一个窗口:Form1; 2、在Form1中...
  • u014056175
  • u014056175
  • 2015年10月14日 09:33
  • 3756

Android View添加到Window的过程

Android 界面显示的过程可以分为两个步骤 1.是将我们要显示的布局添加到window上 2.在进行测量、布局、绘制 通过这两步我们想看到的View就显示在Window上了今天说下View是...
  • hjiangshujing
  • hjiangshujing
  • 2017年04月20日 19:19
  • 383
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Provide an Alternative View to Data Using Splitter Window
举报原因:
原因补充:

(最多只允许输入30个字)