八月 23rd, 2006
- 如果是在CSplitterWnd内,可以如下方法:
CCreateContext context;
context.m_pNewViewClass = pViewClass;
context.m_pCurrentDoc = pDoc;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = NULL;pSplitter->CreateView(row,col,pViewClass,size, &context);
其中pViewClass是你要创建的View类的RUNTIME_CLASS,pSplitter是指向CSplitterWnd的指针。 - 在FrameWnd内,创建作为FrameWnd子窗口的View类:
// the third way, use framewnd.
CCreateContext ccx;
ccx.m_pNewViewClass = RUNTIME_CLASS(CMainView);// here “this” means the framewnd.
m_pMainView = DYNAMIC_DOWNCAST( CMainView, this->CreateView(&ccx) ); - 在其他任何地方创建View类:
CView* pNewView = View*)pViewClass->CreateObject();
pNewView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,
CRect(0,0,0,0),pSplitter,
pSplitter->IdFromRowCol(row, col),&context);
其他:
- 创建完毕后,可以使用如下代码更新显示View:
SetActiveView(pNewView);pSplitter->RecalcLayout();
pNewView->OnInitialUpdate();
pNewView->SendMessage(WM_PAINT); - 可以使用下面的代码删除某个View,比如旧的View:
pDoc->m_bAutoDelete = FALSE;
pOldView->DestroyWindow();
pDoc->m_bAutoDelete = TRUE; - 下面代码可以设置View的ID:
::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, 0);
::SetWindowLong(pNewView->m_hWnd, GWL_ID, pSplitter->IdFromRowCol(row, col)); - 使用GetDlgItem()也可以根据ID来得到一个View的指针,这个是很方便的;
- 可以使用m_pMainView->ResizeParentToFit(FALSE);来使得父窗口从新调整大小。