Thursday, April 14, 2011

How to embed Windows Form in unmanaged ATL GUI?

I have a Windows form user control that is made COM visible. Now I want to embed that control into an ATL dialog GUI.

The ATL project (unmanaged C++) shall only get the progID of the winform and dynamically create and embed it at runtime.

Is this possible and if so, how do I do it?

From stackoverflow
  • I am not sure about ATL but this can be done easily in MFC using CWinFormsView and CWinFormsControl classes.

    I think there is no bulitin support to host a WinForm Control in an ATL Window but I think you can do it by simple getting the HWND of your winform control and setting your ATL control as its parent. This might be a tough road though.

    This seems to be a similar type of thing. I havent tested it myself though.

    Link

    HS : The approach described in the linked page is not applicable, because it is a winform and not an ActiveX. The latter is not really supported in .Net. When using winforms this way, unexpected things are happening.
    HS : When using CWinFormsControl I have to supply an existing winform class name, because it is a template. However, I want to create an arbitraty winform with a given progID. The actual class is not known at compile time!
  • I figured out a way to get it to work.
    The following code is using a CWnd called m_Control that is made to host a winform via a little documented version of CreateControl. Seems to work fine so far. If anyone sees any drawbacks, please comment or respond.

    AfxEnableControlContainer();
    Microsoft::VisualC::MFC::CControlCreationInfoEx i;
    i.Init(System::Type::GetTypeFromProgID(gcnew System::String(sProgID)),
           CControlCreationInfo::ReflectionType);
    i.m_clsid = CLSID_WinFormsControl;
    POINT pt;
    pt.x = pt.y = 0;
    SIZE sz;
    sz.cx = sz.cy = 100;
    m_Control.CreateControl(i, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
                            &pt, &sz, CWnd::FromHandle(m_hWnd), ID_CONTROL);
    
  • should sProgId is of WinFormControl? i didn't find prog id of that, please explain

    HS : You basically make your Winform COMvisible, sign it with a key file and register it via regasm. It then should have a ProgID.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.