Use CTabView multi-view (one polygon)

The book describes the general use CSplitterWnd to split multi-window view, the CreateClient CSplitterWnd can save pCreateContext it creates a pointer to the sub-view shared Document. I explain this with an article in detail. Benefits CTabView establish multiple views that simple label switching allows each View windows are great. Here to talk about how to do this:

In establishing the project, the program does not provide the final step wizard to let View CTabView type, so we need a subclass of their new one CTabView.

In vc2008, in the Project menu "Project" -> "Add Class" can be automatically generated categories, can be added automatically as if the ClassWizard vc6.

WinApp InitInstance modified in the new CMultiDocTemplate (), modify the type just the new View CTabView subclass.

    // Register the application's document templates. Document template 
// serving as a document, the connection between the window and the view frame
CMultiDocTemplate * pDocTemplate;
pDocTemplate = new new CMultiDocTemplate (IDR_TestTabViewTYPE,
the RUNTIME_CLASS (CTestTabViewDoc),
the RUNTIME_CLASS (CChildFrame), // custom MDI child frame
the RUNTIME_CLASS ( CMyTabView )) ;
IF (! pDocTemplate)
return FALSE;
AddDocTemplate (pDocTemplate);

After editing, you can see the form TabView have come out, but there is no sub-View. Let's add a child view.

Usually also need to create sub-view, continue to use the automatic new class can be created CFormView, ClistView like view to display data.

CTabView in view of the new sub-function OnCreate, the need to create a new class to be Doc CCreateContext to the new View.

int CMyTabView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CTabView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CView1);
context.m_pCurrentDoc = GetDocument();

AddView(RUNTIME_CLASS(CView1),"哈哈",100,&context);
context.m_pNewViewClass = RUNTIME_CLASS(CEditView);
AddView(RUNTIME_CLASS(CEditView),"呵呵",101,&context);

return 0;
}

The code of two new view, CCreateContext need to specify a new View, designated as TabView of Doc Doc, in the final pass in the AddView parameters CCreateContext.

Thus, the applet can be used to obtain GetDocument class instance pointer WinApp in CTestTabViewDoc of these sub-views with a shared Doc.

 

Follow-up:

A few days later, I accidentally found AddView code is automatically passed to his son Doc tab, look at the code:

int CTabView::AddView(CRuntimeClass* pViewClass, const CString& strViewLabel, int iIndex /*= -1*/, CCreateContext* pContext/* = NULL*/)
{
ASSERT_VALID(this);
ENSURE(pViewClass != NULL);
ENSURE(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));

CView* pView = DYNAMIC_DOWNCAST(CView, pViewClass->CreateObject());
ASSERT_VALID(pView);

if (!pView->Create(NULL, _T(""), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), &m_wndTabs, (UINT) -1, pContext))
{
TRACE1("CTabView:Failed to create view '%s'\n", pViewClass->m_lpszClassName);
return -1;
}

CDocument* pDoc = GetDocument();
if (pDoc != NULL)
{
ASSERT_VALID(pDoc);

BOOL bFound = FALSE;
for (POSITION pos = pDoc->GetFirstViewPosition(); !bFound && pos != NULL;)
{
if (pDoc->GetNextView(pos) == pView)
{
bFound = TRUE;
}
}

if (!bFound)
{
pDoc->AddView(pView);
}
}

m_wndTabs.InsertTab(pView, strViewLabel, iIndex);

int nTabs = m_wndTabs.GetTabsNum();
return nTabs - 1;
}

Therefore, as long as AddView can no longer needed as a parameter CCreateContext structure, the sub-tab can get a pointer Doc.



Reproduced in: https: //www.cnblogs.com/zhangyonghugo/archive/2011/12/31/CTabView_MutiView.html

Guess you like

Origin blog.csdn.net/weixin_33895016/article/details/93946719