FAQ: 3.2 How do I show WinForms controls in an MFC application?

Answered FAQ: 3.2 How do I show WinForms controls in an MFC application?

All Replies

  • Thursday, May 06, 2010 4:10 AM
     
     Answered

    It depends on whether you are going to show a WinForms control embedded in an MFC dialog or view, or you would like to show a Windows Forms from an MFC application directly.

    1. If you are going to embed a WinForms control in an MFC dialog or view, you can use MFC CWinFormsControl to achieve the objective. The MFC and WinForms Integration download provides Visual C++ 2005 sample demonstrating how to host a WinForms user control as a dialog or a view in an MFC application.

     

    2. If you would like to show a Windows Form from an MFC application directly, the easiest way is to open the /clr build switch for that MFC application. You can do this via Project Properties->Configuration Properties->Project Defaults->Common Language Runtime support. Click that combo box and choose "/clr". Once the setting is configured, you can reference any .NET assembly and use gcnew to initialize a form and show it. You can also add a C++/CLI Windows Form in your project and show it.

     

    Code sample:

    #include "MyWindowForm.h"

    ......

    void CMFCTestDlg::OnBnClickedButton1()

    {

        (gcnew WindowsFormsTest::MyWindowForm())->Show();

    }

     

    Related Thread:

    http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/97fddc35-e94d-4587-8be0-0b573185eb63
    • Marked As Answer by MSDN FAQ Thursday, May 06, 2010 4:10 AM
    •