View和Control的區別(如何在對話方塊上使用CView類)
View和Control的區別(如何在對話方塊上使用CView類)
CView繼承類,和其他視窗類的區別,很重要的就是對CDocument類和CFrameWnd類的操作,而其中,涉及CDocument類的操作,都進行了有效性判斷(m_pDocument != NULL),CView類初始化的時候,m_pDocument = NULL,因此並不影響CView類作為控制元件的使用。涉及CFrame類的操作,有這麼幾個地方:
第一個地方:CView::OnDestroy()。
第二個地方:CView::OnActivateFrame()。
void CView::OnActivateFrame(UINT /*nState*/, CFrameWnd* /*pFrameWnd*/)
{
}
這裡,其實是空的,在CView繼承類中,只有CFormView類繼承了這個虛擬函式
實際上都不需要真的CFrame指標,對CView類作為控制元件使用沒有障礙。
第三個地方:CView::OnMouseActivate()。
另外,在CView::PostNcDestroy(),實現了CView類的自我銷燬,這是因為CView類是可以動態生成的(DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE)。
// self destruction
基本上,修改了CView繼承類的這幾個地方,直接返回不要呼叫基類相應的成員函式,就可以在對話方塊上使用。
下面舉例,用嚮導生成對話方塊應用程式,對話方塊類為CMyDigalog。
從CHTMLView類繼承一個CHTMLCtrl類,建立相應的訊息處理函式並修改以下幾個地方:
增加一個成員函式CreateFromCtrl:
這個函式通過對話方塊上的控制元件建立一個CHTMLCtrl控制元件,目的是在對話方塊設計的時候便於佈局:
還應注意,預設的從CView繼承的類,其建構函式和解構函式是protected的,需要修改成public。
然後,在對話方塊模板中(使用資源編輯器),插入一個Static Text控制元件,ID為IDC_HTML。
在對話方塊標頭檔案中,插入包含檔案:
#include "htmlctrl.h"
增加CHTMLCtrl型別的成員變數:
CHTMLCtrl m_ctlHTML;
在對話方塊初始化的時候,建立這個CHTMLCtrl控制元件:
下面,再新增一個按鈕,在按鈕的訊息響應函式中開啟HTML檔案:
本文參考MSDN上的一篇文章:MSJ 2000中的C++ Q&A,原文有關部分附在後面。
附:原文
What, after all, is the difference between a view and a control? Not much. Both are child windows; the only difference is how they're used. Controls are usually child windows in a dialog—though of course you can create a control as a child of any window you like—whereas views are special child windows designed to work in the MFC doc/view architecture. A view has a pointer to a document and it's designed to live inside a particular kind of window—namely, a frame (CFrameWnd).
As for the document, CView is written so that the document pointer, m_pDocument, can be NULL. Any time the view does something with the document, it encloses the code within
if (m_pDocument!=NULL) { }
So a view doesn't really need a document. Nor does CHtmlView require one. You might think the document in CHtmlView is the HTML file, but in fact CHtmlView is implemented using IWebBrowser2, which has no knowledge of the MFC doc/view architecture.
So CHtmlView doesn't need a document. What about the frame? If you examine the code carefully, you'll discover that there are very few places where a view knows that it belongs to a frame. Most of the doc/view stuff is implemented in higher-level classes such as the frame itself and CDocTemplate, which glues the frame, document, and view together. The view doesn't know too much about what's going on, which shows the system is well-designed. Conceptually, the frame controls the view—not the other way around—so it would be a mistake if the view knew about its parent window. Nevertheless, a little spaghetti wiring always creeps in to any system (usually to fix bugs), and MFC is no exception.
There are two places where CView (and hence CHtmlView by inheritance) assumes it lives inside a frame. The first is CView::OnMouseActivate, the handler for WM_MOUSEACTIVATE. OnMouseActivate does a lot of mumbo-jumbo to make activation work properly when the user clicks the mouse on a view. The details aren't important; the important thing is that the view calls GetParentFrame to get its parent frame, and then CFrameWnd::GetActiveView to activate the active view—all of which assumes the view is a child window of a CFrameWnd.
The other place the view knows it lives in a frame is in CView::OnDestroy.
void CView::OnDestroy() { CFrameWnd* pFrame = GetParentFrame(); if (pFrame != NULL && pFrame->GetActiveView() == this) // deactivate during death pFrame->SetActiveView(NULL); CWnd::OnDestroy(); }
Here the view deactivates itself when it's destroyed. As an aside—something for you to learn from—both of these frame dependencies could be avoided by sending notifications to the parent window instead of calling C++ methods. GetParentFrame could return a CWnd, not a CFrameWnd, since the important thing is that it's the top-level window—not that it's derived from any particular class. And instead of calling CFrameWnd methods, the view could send a notification like WM_IAMGOINGBYEBYENOW, which the "frame" (whether it's a CFrameWnd or a CFooWnd) would be responsible for handling appropriately. After all, it should be the frame, not the view, that decides what to do when a view is activated or destroyed. This is a general rule of thumb in any system; function calls go down (from parent to child), and events go up (from child to parent). A child class should never know what kind of container it lives in.
Ah well, life is never so perfect. Fortunately, MFC's misdemeanors are easily overcome. I wrote a CHtmlCtrl class that's just what you want: an HTML "view" you can use in a dialog or any window. CHtmlCtrl overrides both OnMouseActivate and OnDestroy to bypass the offending CView code.
int CHtmlCtrl::OnMouseActivate(...) { // bypass CView doc/frame stuff return CWnd::OnMouseActivate(...); } void CHtmlCtrl::OnDestroy() { // bypass CView doc/frame stuff CWnd::OnDestroy(); }
Hey, that was easy! The other thing CHtmlCtrl does is override PostNcDestroy.
void CHtmlCtrl::PostNcDestroy() { // Do nothing. Don't let CView get it. }
CView's implementation of PostNcDestroy does a "delete this" to destroy the view that's normal procedure. This is normal procedure for views, which are allocated directly from the heap. But controls customarily live as data members inside some other window object
class CMyDialog ... { CHtmlCtrl m_htmlCtrl; }
in which case you don't want to delete the object in PostNcDestroy because it'll be deleted with the parent object.
相關文章
- c#中的模態對話方塊和非模態對話方塊C#
- Android自定義View(1):對話方塊-DialogAndroidView
- [MFC]選擇目錄對話方塊和選擇檔案對話方塊
- 在 Flutter 使用 GetX 對話方塊Flutter
- 視窗和對話方塊居中對齊
- JavaFX 如何使用內建的對話方塊Java
- 模態對話方塊與非模態對話的幾種銷燬方法與區別薦
- 如何在 Bash Shell 指令碼中顯示對話方塊指令碼
- 對話方塊函式函式
- 登入對話方塊
- VC實現對話方塊上資訊的顯示 (轉)
- React中的模式對話方塊React模式
- flutter demo (四):對話方塊Flutter
- Javascript檔案對話方塊JavaScript
- VUE:點選開啟的對話方塊外面時,對話方塊總是被關閉Vue
- 一個Flex 對話方塊的坑Flex
- javascript中的彈出對話方塊JavaScript
- 如何在Element UI 對話方塊裡面載入高德地圖UI地圖
- Qt下的模態和非模態對話方塊QT
- 關於bootstrap彈出二級對話方塊的使用boot
- Qt 對話方塊新增工具欄QT
- 如何自學qt(4)——對話方塊QT
- QT 等待對話方塊/進度QT
- android 多項對話方塊Android
- Android Dialog對話方塊Android
- DialogPane對話方塊佈局
- 只用html5和css3的modal對話方塊HTMLCSSS3
- 如何在mfc對話方塊中新增一個顯示網頁的視窗 .網頁
- SAP UI5 初學者教程之十五 - 對話方塊和 Fragments 的使用方式UIFragment
- VS2005新建網站對話方塊(檔案系統、HTTP、FTP)的區別網站HTTPFTP
- ASP.NET_頁面中彈出對話方塊幫助類ASP.NET
- 類似QQ對話方塊上下部分可拖動程式碼
- Chrome 對於 JavaScript 對話方塊的新策略 —— 建議不要使用 alert(), confirm(), 和 prompt()ChromeJavaScript
- Flutter Widgets 對話方塊-DialogFlutter
- 常用元件 / 對話方塊及選單元件
- Qt標準對話方塊實現QT
- TWebBrowser禁止彈出Alert對話方塊Web
- android常用對話方塊封裝Android封裝