NET控件的消息处理
一般而言,控件的对话框消息处理是一个极为关键的问题,在网上能找到的MFC中宿主控件的解决方法中,均没有实现.NET控件的对话框消息处理,一个明显的特征是不能处理“Tab”键消息。为此,我们重载了CUserCtrlView的PreTranslateMessage函数:
BOOL CUserCtrlView::PreTranslateMessage(MSG *pMsg) { BOOL bRet = FALSE; if(m_Control.pUnkControl != NULL) { CComQIPtr 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; } 这样可以使得CUserCtrlView可以正确的处理.NET Control的对话框消息。
回归RAD世界
接下来我们看看如何在工程中插入一个.NET用户自定义控件。我们增加一个新的托管类testControl,代码如下:
#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); } }; }
|