doc,view,frame互相呼叫

farcall發表於2011-04-26

訪問應用程式的其它類 
 
 獲得CWinApp: 
 -在CMainFrame,CChildFrame,CDocument,CView中直接呼叫AfxGetApp()或用theApp 
 -在其它類中只能用AfxGetApp() 
 
 獲得CMainFrame: 
 -在CMinApp中用AfxGetMainWnd()或者m_pMainWnd 
 -在CChildFrame中可用GetParentFrame() 
 -在其它類中用AfxGetMainWnd() 
 
 獲得CChildFrame: 
 -在CView中用GetParentFrame() 
 -在CMainFrame中用MDIGetActive()或GetActiveFrame() 
 -在其它類中用AfxGetMainWnd()->MDIGetActive()或AfxGetMainWnd()->GetActiveFrame() 
 
 獲得CDocument: 
 -在CView中用GetDocument() 
 -在CChildFrame中用GetActiveView()->GetDocument() 
 -在CMainFrame中用 
 -if SDI:GetActiveView()->GetDocument() 
 -if MDI:MDIGetActive()->GetActiveView()->GetDocument() 
 -在其它類中 
 -if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument() 
 -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument() 
 
 獲得CView: 
 -在CDocument中 POSITION pos = GetFirstViewPosition();GetNextView(pos) 
 -在CChildFrame中 GetActiveView() 
 -在CMainFrame中 
 -if SDI:GetActiveView() 
 -if MDI:MDIGetActive()->GetActiveView() 
 -在其它類中 
 -if SDI:AfxGetMainWnd()->GetActiveView() 
 -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView() 
 
 ////////////////////////////////////////////////////////////////// 
 不錯!!! 
 
 不過要注意在doc中要取得view的指標C*View要注意類C*View宣告的問題, 
 因為預設情況下,mfc在*View.h中已經包含了*Doc.h,如果在*Doc.h中包含 
 *View.h,就會引起巢狀包含問題,這樣要在*Doc.h中加入 class C*View; 
 而在*Doc.cpp中加入 #include "*View.h" 
 
 ////////////////////////////////////////////////////////////////// 
 其實完全可以在CYourApp中新增各種視或文件的指標,在那些視或文件初 
 始化的時候將指標傳給CYourApp中的對應變數,這樣以後不管在哪用上面 
 指標只需(CYourApp*)AfxGetApp()取其屬性變數即可,明瞭而且清楚更是 
 方便我一直專門操作的說:) 
 
 ////////////////////////////////////////////////////////////////// 
 我先拋塊磚,有玉的砸過來! 
 在何時何地,你都可以通過以下方法精確的得到任何一個物件(Application,DocTemplate,Document,View,Frame) 
 1。通過AfxGetApp()得到當前的App物件; 
 2。通過AfxGetMainWnd()得到主視窗; 
 3。通過CMDIFrameWnd::GetActiveFrame得到當前活動視窗; 
 4。通過GetNextWindow()遍例所有的子視窗;(如果要得到你想要的子視窗,可以通過特定的成員變數來標誌); 
 5。通過CWinApp::GetFirstDocTemplatePostion()以及CWinApp::GetNextDocTemplate()的組合應用來遍歷所有的DocTemplate物件,並且用CDocTemplate::GetDocString()來判斷當前得到的文件莫板物件是哪個。 
 6。通過CDocTemplate::GetFirstDocPosition()以及CDocTemplate的GetNextDoc()組合來遍歷所有的該模板的文件物件,並用CDocument::GetDocTemplate()來得到文件模板,用CDocment::GetTitle() 或者GetPathName()來判斷當前的文件是哪個。 
 7。通過CDocuemt的GetFirstViewPositon()以及GetNextView()來遍歷檢視物件,一般通過訪問View的成員變數來區別各個檢視;通過CView::GetDocument()來得到文件物件; 
 8。Frame->View: 通過GetActiveView方法; 
 9。Frame->Doc:通過GetActiveDocument(); 
 10。View->Frame:GetParentFrame(); 
 11。View->Doc:GetDocuemt()//前面已經說了。 
 12。Doc->View:前面說了; 
 13。Doc->Frame:不知道有沒有很直接的方法。

相關文章