如何在Windows窗体应用程序中显示MFC控件?

问题描述:

我想创建一个Windows窗体控件,该控件显示一个MFC控件(例如CIPAddressCtrl),并带有有效的Text属性和TextChanged事件.如何在Windows窗体应用程序中显示MFC控件?如有必要,我很高兴使用C ++/CLI.

I'd like to create a windows forms control which shows an MFC control such as CIPAddressCtrl, with a working Text property and TextChanged event. How do I display an MFC control in a windows forms application? I'm happy to use C++/CLI if necessary.

注意:我不是在问如何创建全新的Windows窗体控件.我想在Windows窗体应用程序中托管旧版控件.

NOTE: I'm not asking how to create a brand new windows forms control; I want to host a legacy control in a windows forms app.

本文提供了一种解决方案,该解决方案将包装您的MFC控件.巧妙的窍门是在Control :: OnHandleCreated的重写中使用了SubclassWindow.其余的代码涉及使用.NET属性手动包装MFC控件的属性.

This article presents a solution which will wrap your MFC control. The neat trick of this is its use of SubclassWindow in the override of Control::OnHandleCreated. The rest of the code involves manually wrapping the attributes of the MFC control with .NET properties.