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.
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.
In my similar case,
SubclassWindow
inOnHandleCreated
didn't work for some reason. After some struggle, I got it work (without C++/CLI):First, get a HWND from
Form#Handle
and pass it to your MFC DLL.Then, in your DLL,
CWnd::Attach
to the obtained HWND and initialize the control. Clean up withCWnd::Detach
on Dispose.See GuestControl.cs / guest.cpp* for a full example.
Edit: The code in this related question also use
Attach
/Detach
.* The example is my work. (MIT License)
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.