查看msdn可以知道GetActiveFrame其實是CFrameWnd的方法,是MDIFrameWnd繼承而來的方法,在msdn中其說明為:
CFrameWnd::GetActiveFrame
virtual CFrameWnd* GetActiveFrame( );
Return Value
A pointer to the active MDI child window.
If the application is an SDI application, or the MDI frame window has no active document, the implicit
this pointer will be returned.
Remarks
If there is no active MDI child or the application is a single document interface (SDI), the implicit
this pointer is returned.
從說明中我們可以看出,如果改CFrameWnd對象是MDI frame window那麼並且其有active document,那麼該方法的傳回值應該是active MDI child window。這是官方的說明。
我們在看一下MDIGetActive方法的說明:
CMDIFrameWnd::MDIGetActive
CMDIChildWnd* MDIGetActive( BOOL* pbMaximized = NULL ) const; Parameters
-
pbMaximized
-
A pointer to a BOOL return value. Set to TRUE on return if the window is maximized; otherwise FALSE.
Return ValueA pointer to the active MDI child window. |
從上述說明中我們可以看出MDIGetActive是CMDIChildWnd的方法,不是從CFrameWnd繼承而來的,在當前的Frame對象是CMDIFrameWnd對象的話而且有Active document的話,則二者應該是沒有區別的。
但實際情況是否如此呢?至少我今天的經驗告訴我,不是如此。還是那個影像處理演算法架構的問題,昨天將RTTI做好之後,今天加進去了。但是其中在調用的時候出現了問題,由於訊息響應是在一LeftPanel中,不是在Doc中間,所以需要從CAlgorithDialog中間擷取Doc對象,下面是我的兩種擷取方式:
方法一:
CMainFrame* pWnd=(CMainFrame*)AfxGetMainWnd();CImgPlatformDoc* pDoc=(CImgPlatformDoc*)pWnd->MDIGetActive()->GetActiveView()->GetDocument();
方法二:
CMainFrame* pWnd=(CMainFrame*)AfxGetMainWnd();//CImgPlatformDoc* pDoc=(CImgPlatformDoc*)pWnd->MDIGetActive()->GetActiveView()->GetDocument();CImgPlatformDoc* pDoc=(CImgPlatformDoc*)pWnd->GetActiveFrame()->GetActiveView()->GetDocument();
你會發現第二種方法擷取的pDoc實際上是一個NULL值。今天下午在這個問題上犯困了很久,因為一直沒有辦法擷取到Doc對象,一直覺得很奇怪,各種手段都用上了,包括髮送自訂訊息到CMainFrame中,從CMainFrame中調用Doc對象進行處理,但是發現擷取的DOC對象一直是NULL,因為擷取GetActiveFrame的方法一直沒有變,不要和我說你直接用GetActiveDocument方法來擷取DOC對象,我也嘗試過,發現和第二種方法一樣也是空。
到目前為止還沒有找個這兩者之間到底有什麼區別,看到網上很多人說值CMainFrame中直接用GetActiveDocument就可以擷取當前Active的DOC對象了,我表示很懷疑,因為確實會有傳回值,但是你查看對象資料就會探索資料時不對的,而且返回的DOC對象的地址其實是0x00000000也就是說是一個NULL對象。
希望有瞭解這個問題的同學給我講解一下這個問題吧。