您的位置:首页 > 移动开发

如何在MFC的对话框调用CLR控件(From Msdn)

2006-01-05 22:20 781 查看
我们新版本的数据库应用程序目前完全使用VC开发,在我们目前这个小城市里,寻找一个好的VC开发人员实在是不容易,因此,在很早以前,我就开始考虑是否能够通过VC与.NET混合编程,从而在人员招聘上找到更合适的程序员的机会更多一些。我非常高兴的发现VisualStudio2005为我们提供了这样的机会,MFC可以非常方便的调用VB.NET 、C#或其他CLR语言编写的代码,因此我们可以广泛的使用.NET语言编写一些控件,从而提高我们产品的开发效率。
当然,使用VC开发一个大型的数据库应用程序似乎不多见,在处理数据库方面,VB,C#,DELPHI,JAVA都比VC具有优势,我们之所以选择VC开发数据库程序,一是由于我们产品主要是面向医学图像处理,视频处理和视频传输,必须使用VC,公司所有的开发人员都熟悉VC;二是看好了VC开发环境更适合面向对象的方式思考问题,我们大多数人员都通过VC的class view工作,VC的文档视图结构更适合随需应变的数据库管理程序,三是C++具有很多优秀的模板库和更好的泛型设计,关键时候会解决大问题。
下面的一些文字摘自MSDN,介绍了在MFC中如何调用.NET编写的控件。例子中的控件不仅仅适用于用户自定义控件,也可以使用.NET中内置的各种标准控件,例如,我们可以在MFC的对话框中直接调用.Net FrameWork中的TreeView控件。在对话框的头文件中定义一个变量CWinFormsControl<TreeView > m_Tree;在CPP文件的DoDataExchange中增加DDX_ManagedControl(pDX, IDC_TREE, m_Tree);代码

void CVCDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_ManagedControl(pDX, IDC_CTRL1, m_ctrl1);
 DDX_ManagedControl(pDX, IDC_TREE, m_Tree);
}
这样在对话框的初始化函数中可以往TreeView中添加节点了:
TreeNode ^ trRoot=m_Tree.GetControl()->Nodes->Add("根节点");
 TreeNode ^ trChild=trRoot->Nodes->Add("第一个子节点");
运行应用程序,可以看到对话框中显示了添加的树控件和两个节点。

MSDN中的例子如下:

The procedure in this topic assumes you are creating a new dialog-based (CDialog Class) MFC project, but you can also add support for a Windows Form control to an existing MFC dialog.

To create the .NET user control

Create a new Visual C# Windows Control Library project named WindowsControlLibrary1.
From the File menu, select New, then Project. In the Visual C# folder, Select the Windows Control Library icon.
Accept the default project name of WindowsControlLibrary1 by clicking OK.
The default name of the .NET control will be
UserControl1
.

Add child controls to
UserControl1
.
In Toolbox, open the All Windows Forms list. Drag a Button control to the UserControl1 design surface.
Also add a TextBox control.

Change the declarations of the TextBox and Button to public from private in the file UserControl1.Designer.cs.

Build the project.
From the Build menu, click Build Solution.
You may want to note the full path with file name of the generated DLL in the Build log because you will enter that information into the MFC application.

To create the MFC host application

Create a new MFC Application project.
From the File menu, select New, then Project. In the Visual C++ folder, select the MFC Application icon.
In the Name box, enter
MFC01
. Change the Solution setting to Add to Solution. Click OK. The MFC Application Wizard appears.
In the MFC Application Wizard, select Application Type. Choose Dialog based. Accept the remaining defaults and click Finish. This will create an MFC application with an MFC Dialog.

Add a placeholder control to the MFC dialog box.
Click the Resource View tab. In Resource View, double-click on
IDD_MFC01_DIALOG
. The dialog resource appears in Resource Editor.
In Toolbox, open the Dialog Editor list. Drag a Static Text control to the dialog resource. The Static Text control will serve as a placeholder for the .NET Windows Forms control. Resize it to approximately the size of the Windows Forms control.
Change the ID of the Static Text control to
IDC_CTRL1
in the Properties window and change the TabStop property to True.

Configure the project.
In Solution Explorer, right-click the
MFC01
project node, and select Properties from the context menu. The Property Pages dialog box appears.
In the Property Pages dialog box, in the Configuration Properties tree control, select General, then in the Project Defaults section, set Common Language Runtime support to Common Language Runtime Support (/clr). Click OK.

Add a reference to the .NET control.
In Solution Explorer, right-click the
MFC01
project node and select References. In the Property Page, click Add New Reference,select WindowsControlLibrary1 (under the Projects tab), and click OK. This adds a reference in the form of a /FU compiler option so that the program will compile; it also copies WindowsControlLibrary1.dll into the
MFC01
project directory so that the program will run.

In stdafx.h, find this line:
 Copy Code
#endif // _AFX_NO_AFXCMN_SUPPORT
Add these lines above it:
 Copy Code
#include <afxwinforms.h>   // MFC Windows Forms support
Add code to create the managed control.
First, declare the managed control. In MFC01Dlg.h, go to the declaration of the dialog class, and add a data member for the user control in Protected scope as follows:
 Copy Code
class CMFC01Dlg : public CDialog
{
// ...
// Data member for the .NET User Control:
CWinFormsControl<WindowsControlLibrary1::UserControl1> m_ctrl1;
Next, provide an implementation for the managed control. In MFC01Dlg.cpp, in the dialog override of
CMFC01Dlg::DoDataExchange
generated by the MFC Application wizard (not
CAboutDlg::DoDataExchange
, which is in the same file), add the following code to create the managed control and associate it with the static place holder IDC_CTRL1:
 
void CMFC01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_ManagedControl(pDX, IDC_CTRL1, m_ctrl1);
}
Build and run the project.
In Solution Explorer, right-click MFC01 and select Set as StartUp Project.
From the Build menu, click Build Solution.
From the Debug menu, click Start without debugging. You will now see the MFC dialog box displaying the Windows Form control.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息