HOWTO: Province 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 diffition on some run-time conditions.
Steps to create Sample
Create a new SDI project. Add a SplitterWnd (ID_VIEW_SPLITTERWND) menu item to a View menu. This item turns the splitter window on and off. 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;
} 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 ();
} Add a Pointer to a csplitterWnd in the cmainframe definition: Class CMAINFRAME: PUBLIC CFRAMEWND
{
...
protected:
CSplitterWnd * m_psplitter;
...
}; Initialize this Object in The CMAINFRAME CONSTRUctor: CMAINFRAME :: CMAINFRAME ()
{
// Todo: add member initialization code.
m_psplitter = NULL;
}
CMAINFRAME :: ~ cmainframe ()
{
IF (m_psplitter)
Delete m_psplitter;
} Add two new CView-derived classes to your project, representing views that are the panes in your CSplitterWnd Add the following #include statements in your MainFrm.cpp file:. #Include "MyDoc.h"
#include "myview.h"
#include "Pane1View.h"
#include "Pane2View.h" Uses Wizard to create a command and an updateui handler in cmainframe for the menu item you address: 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 it 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 ();
}
PublishedDec 8 1998 9:32 PM Issue Typekbhowto Last Modifedjul 17 2001 4:32 PM Additional Query WordskeywordskbdocView KBMFC KBUIDESIGN KBVC420 KBVC500 KBVC600 KBGRPDSMFCATL