Search code examples
winformsmfcclr

winforms userControl inside MFC with no Clr


I have an MFC application that uses some specific files with CLR but not the whole project.

The challenge is I want to embed a WinForms user control inside the MFC project without the whole project compiled with CLR.

As mentioned before, in one of the existing managed files I created a method that does the exchange with the ctrl like so:

#include <afxwin.h> 
#include <afxwinforms.h>
#include "Managedfile.h"
    
void CWinformsUtil::CreateFullListeUc(CDataExchange* pDX, int Idc)
{
    CWinFormsControl<MainuserControlLib::UserControl2> m_ctrl1;
    DDX_ManagedControl(pDX, Idc, m_ctrl1);
}

And I am calling this method inside the DoDataExchange method in the MFC app source:

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    CWinformsUtil::CreateFullListeUc(pDX, IDC_STATIC1);
}

But the problem with this approach is that it does not display the WinForms user control.


Solution

  • In order to acheive this what i've did was to compile the view.cpp file in wich i want to display the user control in clr,then included <afxwinforms.h> in it .Also in the header file i need to import that library but it should only be visile to managed code by simply making a condition :

        //view.h
        #ifdef _MANAGED
            #include <afxwinforms.h>
        #endif 
    

    and for declaring the control in the header file we need also the same approach:

       //view.h
       #ifdef _MANAGED
            CWinFormsControl<MainuserControlLib::UserControl2> m_ctrl1;
        #endif