写了东西,一个doc对应好多种不同的view
现在需要在么多种view中动态的现实某一个
就是说,现在需要这个view就显示这个,需要那个就显示那个
不需要的就delete掉
怎么实现呢?
现在需要在么多种view中动态的现实某一个
就是说,现在需要这个view就显示这个,需要那个就显示那个
不需要的就delete掉
怎么实现呢?
解决方案 »
- CImageList中add的使用
- visual studio 2010中建立一个MFC单文档,在CMainFram的OnCreate()函数中添加代码时怪异中断。
- 大家试试,先猜猜结果,再在程序里调试看一下结果,与你想得有什么不同
- 如何获得CDateTimeCtrl下拉箭头窗口的指针?
- 使用ADO建立的SQL Server连接怎么会失效?
- 如何让我的程序抢占屏幕?
- 最简单不过的vc问题:关于控件的可视与否的设置。答者即有分得
- 以windows服务程序启动运行一个程序(无窗口),如何弹出窗口(如对话框)
- 高手请进~~~~~~~~~~~~~送分题
- 问:怎样去掉用AppWizard生成的单文档结构的菜单?(VC++)
- 如何使用media player这个控件来播放程序,另外能不能切换声道,也就是说
- vc程序如何在没装vc的机子上运行?
辅助函数:
///////////////////////////////////////////////////////////
// Create a new view based on the document template
// parameter and associated with the given document objectCFrameWnd* UserCreateNewWindow( CDocTemplate* pTemplate,
CDocument* pDocument )
{
ASSERT_VALID( pTemplate );
ASSERT_VALID( pDocument );// 1 - Create the new frame window
// (will in turn create the associated view)
CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, NULL );
if( pFrame == NULL )
{
// Window creation failed
TRACE0( "Warning: failed to create new frame.\n" );
return NULL;
}
ASSERT_KINDOF( CFrameWnd, pFrame );// 2 - Tell the frame to update itself
// (and its child windows)
pTemplate->InitialUpdateFrame( pFrame, pDocument );// 3 - Return a pointer to the new frame window object
return pFrame;
}
立SDI时选择window Style里的use split window,然后调用
CSplitterWnd::CreatStatic来放置你的view
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT ,CCreateContext*pContext)
{
VERIFY(m_wndSplitter.CreateStatic(this,2,1));
VERIFY(m_wndSplitter.CreateView(0,0.RUNTIME_CLASS(ViewA),CSize(100,100),pCon
text);
VERIFY(m_wndSplitter.CreateView(1,0.RUNTIME_CLASS(ViewB),CSize(100,100),pCon
text);
return TRUE;
}
如果你想切换,一次只显示一个视(例如UltraEdit那样,在16进和ASCII间切换),可以
使用SwitchToView
(但我没在MSDN里见过它),它有个enum参数告诉函数切换到那个视图.
enum eView { ViewA = 1, ViewB = 2 };
void SwitchToView(eView nView);
void CMainFrame::SwitchToView(eView nView)
{
CView* pOldActiveView = GetActiveView();
CView* pNewActiveView = (CView*) GetDlgItem(nView);
if (pNewActiveView == NULL) {
switch (nView) {
case ViewA:
pNewActiveView = (CView*) new CViewA;
break;
case ViewB:
pNewActiveView = (CView*) new CViewB;
break;
}
CCreateContext context;
context.m_pCurrentDoc = pOldActiveView->GetDocument();
pNewActiveView->Create(NULL, NULL, WS_BORDER,
CFrameWnd::rectDefault, this, nView, &context);
pNewActiveView->OnInitialUpdate();
}
SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
pOldActiveView->SetDlgCtrlID(
pOldActiveView->GetRuntimeClass() ==
RUNTIME_CLASS(CViewA) ? ViewA : View B);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
RecalcLayout();
}
当然,你也可以用CDocument::AddView和CDocument::RemoveView做,具体可以参见
MSDN的Sample
BOOL CMainFrame::OnViewChange(UINT nCmdID)
{
CView* pViewAdd;
CView* pViewRemove;
CDocument* pDoc = GetActiveDocument();
UINT nCmdID;
nCmdID = LOWORD(GetCurrentMessage()->wParam);
if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1))
return;
if((nCmdID == ID_VIEW_VIEW2) && (m_currentView == 2))
return;
if (nCmdID == ID_VIEW_VIEW2)
{
if (m_pView2 == NULL)
{
m_pView1 = GetActiveView();
m_pView2 = new CMyView2;
m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL);
}
pViewAdd = m_pView2;
pViewRemove = m_pView1;
m_currentView= 2;
}
else
{
pViewAdd = m_pView1;
pViewRemove = m_pView2;
m_currentView= 1;
}
// 将活动视的child id设置为AFX_IDW_PANE_FIRST
// 将其它视设置为AFX_IDW_PANE_FIRST以外的值,
// 这样当调用 CFrameWnd::RecalcLayout重新布局窗口时,
// 才会得到正确的视
int nSwitchChildID = pViewAdd->GetDlgCtrlID();
pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pViewRemove->SetDlgCtrlID(nSwitchChildID);
// 显示活动视而隐藏非活动视
pViewAdd->ShowWindow(SW_SHOW);
pViewRemove->ShowWindow(SW_HIDE);
// 将新的活动视连接到文档,并断开原来的视与文档的连接
pDoc->AddView(pViewAdd);
pDoc->RemoveView(pViewRemove);
SetActiveView(pViewAdd);
RecalcLayout();
}
CView* CTestDoc::GetView(CRuntimeClass* pClass)
{
CView* pView;
POSITION pos=GetFirstViewPosition(); while(pos!=NULL){
pView=GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
} if(!pView->IsKindOf(pClass)){
AfxMessageBox("Connt Locate the View.\r\n http://www.msdn.com");
return NULL;
} return pView;
}2、从一个视图类取得另一视图类的指针
(假设要从CTestAView中取得指向其它视图类的指针)
CView* CTestAView::GetView(CRuntimeClass* pClass)
{
CTestDoc* pDoc=(CTestDoc*)GetDocument();
CView* pView;
POSITION pos=pDoc->GetFirstViewPosition();
while(pos!=NULL){
pView=pDoc->GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
}
if(!pView->IsKindOf(pClass)){
AfxMessageBox("Connt Locate the View.");
return NULL;
} return pView;
}