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_str
User-DefinedNameText
Property to bind DDX/DDV data.
When First callCMyDlg::DoDataExchange
Create the control. Therefore, referencem_UserControl
Any 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:
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.