天天看點

VC的MDI中實作動态切換文檔視圖

今天要實作一個功能,如下圖所示:

VC的MDI中實作動态切換文檔視圖

在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;
}
           

上述代碼中屏蔽部分與為屏蔽部分都實作了激活視窗功能。

第三步:如果不存在,則建立一個視圖,并初始化該視圖。這裡不貼出具體代碼了。實作步驟與建立視圖的步驟是一樣的的,隻不過添加了初始化的工作。

最後感謝一位朋友幫了我的大忙,沒有他的話這個功能還得晚幾天才能出來!@輔_, ,感謝你!

繼續閱讀