views:

106

answers:

1

Using C++/CLI, How to display a managed control (eg. System::Windows::Forms::Panel^) on a window created in native code?

An external program calls my native method where i can access it's window via

SubclassWindow(hNativeWindow, MyNativeWindowProc);

Then I create control with something similar to:

MyNameSpace::MyControl^ ctrl = osozKomunikator = gcnew MyControl("SomeText", hNativeWindow);
ctrl->Show();

MyControl is derived from System::Windows::Forms::UserControl and has overriden CreateParams to set Parent to hNativeWindow.

As the result the control flashes and dissapears, does not show at all or shows only after I slow down the execution with the debugger.

Please help.

+1  A: 

Windows Forms supports being hosted like ActiveX in native windows since .Net 1.1. The host needs to implement some interfaces, though. MFC 8.0 wrapped around the necessary code in CWinFormsDialog and CWinFormsView. Use MFC's support classes if you can. If you can not, install MFC from Visual C++ 2005 or higher and check the source code of MFC's OLE support classes, like COleControlContainer, COleControlSite, etc.

Sheng Jiang 蒋晟