使用CreateControl(...)函数建立.Net控件,在Debug下可以运行,在Release下无法运行
牧童吃五谷 2019-02-21 11:26:56 想在MFC对话框程序中动态建立一个.Net控件。
流程如下:
1.建立MFC对话框程序
2.给程序添加公共语言运行时支持,即支持CLR
2.添加一个窗口类CActiveXProxyWnd
///// ActiveXProxyWnd.h文件内容如下
class CActiveXProxyWnd : public CWnd
{
protected:
DECLARE_DYNCREATE(CActiveXProxyWnd)
public:
CActiveXProxyWnd();
virtual ~CActiveXProxyWnd();
virtual BOOL Create_wfh(CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
DECLARE_MESSAGE_MAP()
};
///// ActiveXProxyWnd.cpp文件内容如下
#include <afxwinforms.h>
IMPLEMENT_DYNCREATE(CActiveXProxyWnd, CWnd)
BEGIN_MESSAGE_MAP(CActiveXProxyWnd, CWnd)
END_MESSAGE_MAP()
CActiveXProxyWnd::CActiveXProxyWnd()
{
}
CActiveXProxyWnd::~CActiveXProxyWnd()
{
}
BOOL CActiveXProxyWnd::Create_wfh(CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
CRect rect2(0, 0, 200, 100);
CPoint pt = rect2.TopLeft();
CSize size = rect2.Size();
System::Type^ TestType = System::Type::GetType(L"System.Windows.Forms.Button,System.Windows.Forms,Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089");
Microsoft::VisualC::MFC::CControlCreationInfoEx info;
info.Init(TestType, CControlCreationInfo::ReflectionType);
info.m_clsid = CLSID_WinFormsControl;
BOOL Ret = CreateControl(info, WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN, &pt, &size, pParentWnd, nID);
DWORD dd = ::GetLastError();
CString sss;
sss.Format(L"CreateControl返回%d\r\nGetLastError返回=%d", Ret, dd);
AfxMessageBox(sss);
return Ret;
}
4.在主对话框窗口类中添加一个类成员
CActiveXProxyWnd ActiveXProxyWnd;
5.在对话框上面增加一个按钮,响应按钮的点击执行
ActiveXProxyWnd.Create_wfh(this, 31234, nullptr);
现在的问题是在debug版本编译运行时,能够动态生成需要的.Net按钮控件,如果采用release版本编译,则不能生成.Net按钮控件。
望各位高手帮忙解决!!!