Host Windows Forms user controls in the form of an MFC dialog box

Source: Internet
Author: User
Visual c ++ Host Windows Forms user controls in the form of an MFC dialog box

MFC provides the template class cwinformsdialog. You can use this class to host the Windows Forms user control (usercontrol) in the mode or mode-free MFC dialog box ).CwinformsdialogIs derived from the MFC class cdialog, so this dialog box can be started in mode or mode without mode.

CwinformsdialogThe process of carrying a user control is similar to that described in the Windows form user control in the MFC dialog box. However,CwinformsdialogManage the initialization and bearer of user controls, so you do not have to program them manually.

For example applications that display windows forms used with MFC, see "MFC and winforms integration" (integration of MFC and winforms ).

Create an MFC Host application
  1. Create a new MFC application project.

    On the File menu, selectNewAnd then click Project ". In the Visual C ++ folder, select "MFC application ".

    In the Name box, enterMfc03And change the "solution" settings to "add SOLUTION ". Click OK ".

    In the "MFC Application Wizard", accept all default values and click "finish ". This creates an MFC application with a multi-document interface.

  2. Configuration item.

    In Solution Explorer, right-clickMfc03Project node, and then select "properties" from the context menu ". The "property page" dialog box appears.

    In the "properties" dialog box, select "general" in the "Configure properties" tree control, and in the "project default" section, set "Public Language Runtime Library support" to "Public Language Runtime Library support (/CLR )". Click OK ".

  3. Add a reference to the. NET control.

    In Solution Explorer, right-clickMfc03Project node, and then select "Reference ". On the properties page, click "Add new reference", select "windowscontrollibrary1" (under the "project" tab), and click "OK ". This operation adds a reference in the form of/Fu compiler options for program compilation; it also copies windowscontrollibrary1.dllMfc03Project directory for program running.

  4. Set# Include <afxwinforms. h>Add to stdafx. h existing# IncludeStatement.

  5. AddCdialogThe new class of the subclass.

    Right-click the project name and addCdialogThe MFC class of the subclass (named chostforwinform ). Because you do not need resources in the dialog box, you can delete the resource ID (select "resource view", expand the "dialog box" folder, and delete the idd_hostforwinform resource. Delete all references to this ID in the Code .)

  6. In the chostforwinform. h and chostforwinform. cpp filesCdialogReplaceCwinformsdialog <windowscontrollibrary1: usercontrol1>.

  7. Call domodal for the chostforwinform class.

    In mfc03.cpp, add# Include "hostforwinform. H".

    Before the return statement in cmfc03app: initinstance definition, add:

    Chostforwinform m_hostforwinform;

    M_hostforwinform.domodal ();

  8. Generate and run this project.

    On the "generate" menu, click "generate SOLUTION ".

    On the Debug menu, click Start (do not Debug )".

    Next, we will add code to monitor the control status on Windows forms from the MFC application.

  9. Add the oninitdialog handler.

    The "properties" window (F4) is displayed ). In the Class View, select chostforwinform ". In the "properties" window, select overrides, and in the row where oninitdialog is located, click the left column and select "<add> ". This adds the following to chostforwinform. h:

    Copy code
    virtual BOOL OnInitDialog();
  10. Define oninitdialog as follows (in chostforwinform. cpp:

    Copy code
    BOOL CHostForWinForm::OnInitDialog() {   CWinFormsDialog<WindowsControlLibrary1::UserControl1>::OnInitDialog();   GetControl()->button1->Click += MAKE_DELEGATE(System::EventHandler, OnButton1);   return TRUE;}
  11. Next, add the onbutton1 handler. Add the following lines to the public section of the chostforwinform class in chostforwinform. h:

    Copy code
    virtual void OnButton1( System::Object^ sender, System::EventArgs^ e );BEGIN_DELEGATE_MAP( CHostForWinForm )   EVENT_DELEGATE_ENTRY( OnButton1, System::Object^, System::EventArgs^ );END_DELEGATE_MAP()

    In chostforwinform. cpp, add the following definition:

    Copy code
    void CHostForWinForm::OnButton1( System::Object^ sender, System::EventArgs^ e ) {   System::Windows::Forms::MessageBox::Show("test");}
  12. Generate and run this project. When you click this button (on a Windows form), the code in the MFC application is run.

    Next, we will add code to display the value in the text box on Windows form from the MFC code.

  13. In the public section of the chostforwinform class in chostforwinform. H, add the following declaration:

    Copy code
    CString m_sEditBoxOnWinForm;
  14. In the dodataexchange definition in chostforwinform. cpp, add the following three lines to the end of the function:

    Copy code
    if (pDX->m_bSaveAndValidate)   m_sEditBoxOnWinForm = CString( GetControl()->textBox1->Text);else   GetControl()->textBox1->Text = gcnew System::String(m_sEditBoxOnWinForm);
  15. In the onbutton1 definition in chostforwinform. cpp, add the following three lines to the end of the function:

    Copy code
    this->UpdateData(TRUE);System::String ^ z = gcnew System::String(m_sEditBoxOnWinForm);System::Windows::Forms::MessageBox::Show(z);
  16. Generate and run this project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.