Control binding and message ing in MFC and. Net interoperability

Source: Internet
Author: User
Recently, I was busy with development work, so I didn't have time to access the Internet. Today I saw a friend asking me about MFC and. net interoperability message ing issues, in fact, the msdn help has a very detailed description, I Will paste the relevant content here:
Data Binding
 

Call To create a control that matches the resource control ID. If you use ddx_managedcontrol for the cwinformscontrol control (in the code generated by the wizard), you cannot explicitly call createmanagedcontrol for the same control.

In You can call ddx_managedcontrol to create a Control Based on the Resource ID. For data exchange, you do not need to use DDX/DDV functions for Windows Forms controls. You can insert code in the dodataexchange method of the dialog box (or view) class to access the properties of the managed control, as shown in the following example.

The following example shows how to bind a local C ++ string to A. Net user control.

Example

The following example is an MFC stringm_strUser-DefinedNameTextProperty to bind DDX/DDV data.

When First callCMyDlg::DoDataExchangeCreate the control. Therefore, referencem_UserControlAny code of must be called after ddx_managedcontrol.

You can implement this code in the mfc01 application created in the dialog box by creating a user control and carrying it.

Put the following code in the cmfc01dlg declaration:

  Copy code
class CMFC01Dlg : public CDialog
{
CWinFormsControl<WindowsControlLibrary1::UserControl1> m_MyControl;
CString m_str;
};

Put the following code in the implementation of cmfc01dlg:

  Copy code
void CMFC01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_ManagedControl(pDX, IDC_CTRL1, m_MyControl);

if (pDX->m_bSaveAndValidate) {
m_str = m_MyControl->textBox1->Text;
} else
{
m_MyControl->textBox1->Text = gcnew System::String(m_str);
}
}

Now, we will click "OK" to add the handler method. Click the "resource view" tab. In "resource view", double-clickIDD_MFC01_DIALOG. The dialog box resources are displayed in the resource editor. Double-click OK.

Define the handler as follows.

  Copy code
void CMFC01Dlg::OnBnClickedOk()
{
AfxMessageBox(CString(m_MyControl.GetControl()->textBox1->Text));
OnOK();
}

Add the downstream to the implementation of bool cmfc01dlg: oninitdialog.

  Copy code
   m_MyControl.GetControl()->textBox1->Text = "hello";

You can now generate and run an application. Note that when the application is closed, any text in the text box is displayed in the pop-up message box.

Message ing

 

You can enable the C ++ class of the Local Machine to receive callbacks from managed events caused by Windows form controls or other forms with the MFC macro ing format. Events in the receiving view or dialog box are similar to events in the receiving control.

To do this, you must:

  • Use Append The onclick event handler to the control.

  • Use , And Create a delegate ing.

Example

This example continues the work you completed in how to: use Windows Forms to execute DDX/DDV data binding.

Now (m_MyControl) And hosting The event's managed event handler delegate named onclick is associated.

Add the first line of code to the implementation of bool cmfc01dlg: oninitdialog.

Add the delegate ing and onclick definitions to the public section in the cmfc01dlg class declaration: Public cdialog.

  Copy code
m_MyControl.GetControl()->button1->Click += MAKE_DELEGATE( System::EventHandler, OnClick );

// delegate map
BEGIN_DELEGATE_MAP( CMFC01Dlg )
EVENT_DELEGATE_ENTRY( OnClick, System::Object^, System::EventArgs^ )
END_DELEGATE_MAP()

void OnClick( System::Object^ sender, System::EventArgs^ e ) {}

In fact, there is a detailed explanation of the interoperability issue in msdn. You only need to enter the word "interoperability" in the msdn index to find all the articles and descriptions related to interoperability. You only need to write an example program according to the msdn instructions to understand the relevant content.

For my next time, because I want to complete the development of a new product, one of the Report Program controls is written in C #, and I need to call it in the MFC multi-file main program, therefore, interoperability is required. In the future, it is also expected that the program supports scripts and secondary development, and interoperability is also required.

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.