(圖3. Add Class向?qū)В涸黾油泄蹸++ Component對象) 添加了該testDocObject托管組件對象之后,將該對象的基類改為Object,并刪除一些代碼得到一個最小托管類:| 以下是引用片段: namespace test { __gc public class testDocObject : public Object { public: testDocObject(void) { } }; } |
| 以下是引用片段: public: gcroot<test::testDocObject*> m_ptestDocObj; |
| 以下是引用片段: BOOL CtestDoc::InitialDocument() { # #undef new m_ptestDocObj = new test::testDocObject(); #pragma pop_macro("new") } |
(圖4:MFC框架中ActiveX控件的創(chuàng)建) 我們知道,MFC是通過COleControlSite類創(chuàng)建ActiveX控件的,由于針對用于ActiveX控件的COleControlSite類不適用于.NET控件,因此必須重新派生一個新類CWFControlSite來提供必要的支持,通過一個CWFControlWrapper類將一個.NET控件包裝在一個CWnd窗體中,并將包裝后的窗體“安置”在CWFControlSite內(nèi)。CWFControlWrapper類代碼如下:| 以下是引用片段: class CWFControlWrapper : public CWnd { public: CWFControlWrapper(); virtual ~CWFControlWrapper(void); IUnknown *pUnkControl; IUnknown *GetManagedControl() { return pUnkControl; } void SetControlSite(COleControlSite *pSite) { m_pCtrlSite = pSite; } }; |
| 以下是引用片段: BOOL CUserCtrlView::PreTranslateMessage(MSG *pMsg) { BOOL bRet = FALSE; if(m_Control.pUnkControl != NULL) { CComQiptr<IOleInPlaceActiveObject> spInPlace(m_Control.pUnkControl); if(spInPlace) bRet =(S_OK == spInPlace-> TranslateAccelerator(pMsg)) ? TRUE : FALSE; } if(CView::PreTranslateMessage(pMsg)) return TRUE; CFrameWnd *pFrameWnd = GetTopLevelFrame(); if(pFrameWnd != NULL && pFrameWnd->m_bHelpMode) return FALSE; // start with first parent frame pFrameWnd = GetParentFrame(); while(pFrameWnd != NULL) { if(pFrameWnd->PreTranslateMessage(pMsg)) return TRUE; pFrameWnd = pFrameWnd->GetParentFrame(); } return bRet; } |
| 以下是引用片段: #pragma once ... namespace test { public __gc class testControl : public System::Windows::Forms::UserControl { public: testControl(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if(disposing && components) components->Dispose(); __super::Dispose(disposing); } private: System::Windows::Forms::Label *label1; System::ComponentModel::Container *components; void InitializeComponent(void) { this->label1 = new System::Windows::Forms::Label(); this->SuspendLayout(); this->label1->Location = System::Drawing::Point(16, 24); this->label1->Name = S"label1"; this->label1->Size = System::Drawing::Size(208, 16); this->label1->TabIndex = 0; this->label1->Text = S"Welcome to TZ MFC.NET!"; this->Controls->Add(this->label1); this->Name = S"testControl"; this->Size = System::Drawing::Size(240, 160); this->ResumeLayout(false); } }; } |
新聞熱點
疑難解答