Summary of the static split window of a single document that can switch views

Source: Internet
Author: User
After multiple parties searched for data and repeated experiments, we finally achieved a window separation in a single document program, and we can switch the view at will. The following are detailed steps:

1. Step 4 of the Wizard. Select "advanced" and select "use split column". The original VIEW class is CMyView, which is derived from the CVIEW class.
2. To add a class derived from other VIEW classes, such as CFormView, add # include <afxcview. h> to StdAfx. h.
3. Add the new class CTView, which is derived from CTreeView and edit the initialization CTView: OnInitialUpdate () Code as follows:

Void CTView: OnInitialUpdate ()
{
CTreeView: OnInitialUpdate ();

// TODO: Add your specialized code here and/or call the base class
CTreeCtrl & treeCtrl = GetTreeCtrl ();
DWORD dwStyle =: GetWindowLong (treeCtrl. m_hWnd, GWL_STYLE );
DwStyle | = TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
: SetWindowLong (treeCtrl. m_hWnd, GWL_STYLE, dwStyle );
HTREEITEM hRoot, hCurPos;
TV _INSERTSTRUCT tInsert;
TInsert. hParent = TVI_ROOT;
TInsert. hInsertAfter = TVI_LAST;
TInsert. item. mask = TVIF_TEXT | TVIF_PARAM;
TInsert. item. pszText = "category ";
TInsert. item. lParam = 0;
HRoot = treeCtrl. InsertItem (& tInsert );
Char * plant [4] = {"programming", "Fiction", "science", "humanities "};
Char * cell [4] [5] = {
{"VC ++", "Delphi", "BCB", "", ""}, // main system running log
{"Martial Arts", "detective", "romance", "terrorism", "suspense "},
{"Astronomy", "Geography", "nature ","",""},
{"Social science ","","","",""}
};
Int I, j;
For (I = 0; I <4; I ++)
{
TInsert. hParent = hRoot;
TInsert. item. pszText = plant [I];
HCurPos = treeCtrl. InsertItem (& tInsert );
For (j = 0; j <5; j ++)
{
TInsert. hParent = hCurPos;
If (cell [I] [j]! = "")
{
TInsert. item. pszText = cell [I] [j];
TreeCtrl. InsertItem (& tInsert );}
}
// TreeCtrl. Expand (hCurPos, TVE_EXPAND );

}
TreeCtrl. Expand (hRoot, TVE_EXPAND );

}

4. Add a CFormView derived class. First, add a dialog box resource. In the dialog box properties, specify STYLES-> STYLE: child.
BORDER: NONE. The Title Bar is not selected and is not visible in More Styles. Then, specify the dialog box as the created dialog box in the CFView of the newly created CFormView derived class.

5. Because each CView derived class has inherited the GetDocument () function, you do not need to declare the GetDocument () function in it as long as you call it directly, after the call, perform type forced conversion. For example, comment out
// Attributes
// CMyDoc * GetDocument ();
Comment out in cmyview. cpp
// CMyDoc * CMyView: GetDocument () // non-debug version is inline
//{
// ASSERT (m_pDocument-> IsKindOf (RUNTIME_CLASS (CMyDoc )));
// Return (CMyDoc *) m_pDocument;
//}

And try the following code in OnDraw:
Void CMyView: OnDraw (CDC * pDC)
{
CMyDoc * pDoc = (CMyDoc *) CMyView: GetDocument ();
ASSERT_VALID (pDoc );
// TODO: add draw code for native data here
}

7. To enable the newly added view derived class to be conveniently called, you can change the constructor of these view derived classes from protected to public.

8. Add a new CMySplitter class derived from CMDIChildWnd class, and change all javasichildwnd in the code to CSplitterWnd.
Add # include "MySplitter. h" to MainFrm. h and change CSplitterWnd m_wndSplitter
CMySplitter m_wndSplitter;

9. Add a publice member function to the CMySplitter class.
Bool replaceview (INT row, int Col, cruntimeclass * pviewclass, size );
Edit the Code as follows:

Bool cmysplitter: replaceview (INT row, int Col, cruntimeclass * pviewclass, size)
{
Ccreatecontext context;
Bool bsetactive;


If (getpane (row, col)-> iskindof (pviewclass) = true)
Return false;


// Get pointer to cdocument object so that it can be used in the creation
// Process of the new view
Cdocument * pdoc = (cview *) getpane (row, col)-> getdocument ();
CView * pActiveView = GetParentFrame ()-> GetActiveView ();
If (pActiveView = NULL | pActiveView = GetPane (row, col ))
BSetActive = TRUE;
Else
BSetActive = FALSE;

// Set flag so that document will not be deleted when view is destroyed
PDoc-> m_bAutoDelete = FALSE;
// Delete existing view
(CView *) GetPane (row, col)-> DestroyWindow ();
// Set flag back to default
PDoc-> m_bAutoDelete = TRUE;
 
// Create new view

Context. m_pNewViewClass = pViewClass;
Context. m_pCurrentDoc = pDoc;
Context. m_pNewDocTemplate = NULL;
Context. m_pLastView = NULL;
Context. m_pCurrentFrame = NULL;

CreateView (row, col, pViewClass, size, & context );

CView * pNewView = (CView *) GetPane (row, col );

If (bSetActive = TRUE)
GetParentFrame ()-> SetActiveView (pNewView );

RecalcLayout ();
GetPane (row, col)-> SendMessage (WM_PAINT );

Return TRUE;
}

10. Add the value in front of mainfrm. cpp.
# Include "appdoc. H"
# Include "tview. H"
# Include "myview. H"
# Include "fview. H"
Edit the virtual function bool cmainframe: oncreateclient in mainfrm. cpp as follows:

Bool cmainframe: oncreateclient (lpcreatestruct/* lpcs */,
Ccreatecontext * pcontext)
{

 
If (! M_wndsplitter.createstatic (this, 1, 2 ))
{
Trace (_ T ("failed to create the splitter "));
Return false;
}

If (! M_wndsplitter.createview (100,100, runtime_class (ctview), csize (), pcontext ))
{
Trace (_ T ("failed to create view in first pane "));
Return FALSE;
}

If (! M_wndSplitter.CreateView (100,100, RUNTIME_CLASS (CMyView), CSize (), pContext ))
{
TRACE (_ T ("failed to create view in second pane "));
Return FALSE;
}

 
Return TRUE;
}

11. Add two menu items ID_VIEW_VIEW and ID_VIEW_FVIEW to correspond to two views respectively, and add member variables to MainFrm. h.
BOOL ShowView1; return 0 in CMainFrame: OnCreate; add a line in front of ShowView1 = true;

The code for editing two menu items is as follows:

Void CMainFrame: OnViewView ()
{
// TODO: Add your command handler code here
CMainFrame * pMainFrame = (CMainFrame *) AfxGetMainWnd ();
PMainFrame-> m_wndSplitter.ReplaceView (100,100, RUNTIME_CLASS (CSplView), CSize ));
ShowView1 = true;
}

Void CMainFrame: OnViewFview ()
{
// TODO: Add your command handler code here
CMainFrame * pMainFrame = (CMainFrame *) AfxGetMainWnd ();
PMainFrame-> m_wndSplitter.ReplaceView (100,100, RUNTIME_CLASS (CFView), CSize ));
ShowView1 = false;
}

Void CMainFrame: OnUpdateViewView (CCmdUI * pCmdUI)
{
// TODO: Add your command update UI handler code here
PCmdUI-> SetCheck (ShowView1 );
}

Void CMainFrame: OnUpdateViewFview (CCmdUI * pCmdUI)
{
// TODO: Add your command update UI handler code here
PCmdUI-> SetCheck (! ShowView1 );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.