VC的MDI中实现动态切换文档视图

今天要实现一个功能,如下图所示:

VC的MDI中实现动态切换文档视图_第1张图片
在1中点击Screen0,然后2中的视图如果视图Screen0存在,则激活Screen0窗口,如果不存在这新建一个Screen0窗口并显示。

实现代码:

第一步:判断树形控件所按下时的文件名

void CViewTree::OnClkTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	CPoint pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);

	UINT uFlag = 0;
	HTREEITEM hItem = HitTest(pt, &uFlag);

	if(hItem)
	{
		SelectItem(hItem);
		if(TVHT_ONITEMLABEL & uFlag)
		{
			CString szFileName; 
			CString szFilePath;
			CString szNewFileName;
			BOOL bIsViewOpened = TRUE ;

			szFileName= GetItemText(hItem); 
			int nIndex = szFileName.Find(".");
			szNewFileName = szFileName.Left(nIndex);

			CFileView * pView = (CFileView *)GetParent();			
			pView->FindFileNode(szFileName, szFilePath, bIsViewOpened);

			if (szFilePath != "" /*&& !bIsViewOpened*/)
			{
				if (bIsViewOpened)
				{
					FindDocment(szNewFileName);
				}
				else
					pView->OpenFileNode(szNewFileName, szFilePath);				
			}		
		}
	} 

	*pResult = 0;
}


第二部:判断该文件名所对应的同名视图是否存在,存在就激活该窗口

CCFDesignView * CViewTree::FindDocment(CString szName)
{
 	CDocManager * pDocManager = AfxGetApp()->m_pDocManager;
 	POSITION posDocManager = pDocManager->GetFirstDocTemplatePosition();
 	while(posDocManager != NULL)
 	{
 		CDocTemplate * pDocTemplate = (CDocTemplate *)pDocManager->GetNextDocTemplate(posDocManager);
 		POSITION posDoc = pDocTemplate->GetFirstDocPosition();
 		
 		while(posDoc != NULL)
 		{
 			CDocument * pDoc = pDocTemplate->GetNextDoc(posDoc);		
 
 			if (pDoc->GetTitle() == szName)
 			{	
 				POSITION posView = pDoc->GetFirstViewPosition();
 				
 				while (posView != NULL)
 				{
 					CView * pView = pDoc->GetNextView(posView);
//   					CMainFrame * pMainFrame = (CMainFrame *)AfxGetMainWnd();
//   					pMainFrame->SetActiveView(pView);	
					CChildFrame * pChildFrame = (CChildFrame *)pView->GetParentFrame();
					pChildFrame->ActivateFrame(SW_SHOW);
 				}
 			}
			//pDoc->UpdateAllViews(NULL);
 		}
 	}

// 	CWinApp *pApp = AfxGetApp();
// 	POSITION PosDocTemplate = pApp->GetFirstDocTemplatePosition();
// 	while(PosDocTemplate)
// 	{
// 		CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate( PosDocTemplate );
// 
// 		POSITION PosDoc = pDocTemplate->GetFirstDocPosition();
// 		while(PosDoc)
// 		{
// 			CDocument* pDoc = pDocTemplate->GetNextDoc( PosDoc );
// 
//   			if (pDoc->GetTitle() == szName)

//   			{	

// 				POSITION PosView = pDoc->GetFirstViewPosition();
// 				while(PosView)
// 				{
// 					CCFDesignView* pView = (CCFDesignView *)pDoc->GetNextView( PosView );
// 					//CMainFrame * pMainFrame = (CMainFrame *)AfxGetMainWnd();

// 					//pMainFrame->SetActiveView(pView, TRUE);	

// 					//CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;

// 					//pFrame->SetActiveView(pView);

// // 					CFrameWnd* pParentFrame=(CFrameWnd*)pView->GetParent();

// // 					pParentFrame->SetActiveView(pView);

// // 					CChildFrame * pChildFrame = (CChildFrame *)pView->GetParentFrame();

// // 					pChildFrame->ActivateFrame(SW_SHOW);

// 					return NULL;

// 				}

//   			}
// 
// 		}
	//}


	return NULL;
}
上述代码中屏蔽部分与为屏蔽部分都实现了激活视窗功能。

第三步:如果不存在,则新建一个视图,并初始化该视图。这里不贴出具体代码了。实现步骤与新建视图的步骤是一样的的,只不过添加了初始化的工作。

最后感谢一位朋友帮了我的大忙,没有他的话这个功能还得晚几天才能出来!@辅_, ,感谢你!


你可能感兴趣的:(windows,mfc,MDI,CTreeCtrl,文档视图)