How to call the CLR control (from msdn) in the MFC dialog box)

Source: Internet
Author: User

Our new version of database applications is currently fully developed using VC. In our current small city, it is not easy to find a good VC developer. Therefore, a long time ago, I began to consider whether it was possible to use VC and. net hybrid programming, so as to find more suitable programmers opportunities in recruitment. I am very happy to find that visualstudio2005 provides us with this opportunity. MFC can easily call VB.. net, C #, or code written in other CLR languages, so we can use it extensively.. NET language to write some controls to improve the efficiency of our product development.

Of course, it seems rare to use VC to develop a large database application. In terms of database processing, VB, C #, Delphi, and Java have advantages over VC. We chose VC to develop database programs, first, because our products are mainly for medical image processing, video processing and video transmission, VC must be used, and all the company's developers are familiar with VC; second, the VC development environment is better suited for object-oriented thinking. Most of us work through the VC Class View, the VC document view structure is more suitable for on-demand database management programs. Third, C ++ has many excellent template libraries and better generic design, which will solve major problems in key cases.

The following text is taken from msdn and describes how to call the. NET control in MFC. The controls in the example are not only applicable to user-defined controls, but also can use various built-in standard controls in. net. For example, we can directly call the Treeview control in. NET Framework in the dialog box of MFC. Define a variable cwinformscontrol <Treeview> m_tree in the header file of the dialog box; add ddx_managedcontrol (PDX, idc_tree, m_tree) to dodataexchange of the CPP file; Code

Void cvcdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
Ddx_managedcontrol (PDX, idc_ctrl1, m_ctrl1 );
Ddx_managedcontrol (PDX, idc_tree, m_tree );
}

In this way, you can add nodes to the Treeview in the initialization function of the dialog box:

Treenode ^ trroot = m_tree.getcontrol ()-> nodes-> Add ("Root Node ");
Treenode ^ trchild = trroot-> nodes-> Add ("first subnode ");

Run the application. The added tree control and two nodes are displayed in the dialog box.

The example in msdn is as follows:

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
  1. Create a New Visual C # windows control library project named windowscontrollibrary1.

    FromFileMenu, selectNew, ThenProject. InVisual C #Folder, selectWindows Control LibraryIcon.

    Accept the default project name of windowscontrollibrary1 by clickingOK.

    The default name of the. NET control will beUserControl1.

  2. ADD child controlsUserControl1.

    InToolbox, OpenAll Windows FormsList. Drag a button control to the usercontrol1 design surface.

    Also add a Textbox Control.

  3. Change the declarations of the textbox and button to public from private in the file usercontrol1.designer. CS.

  4. Build the project.

    FromBuildMenu, clickBuild 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
  1. Create a new MFC application project.

    FromFileMenu, selectNew, ThenProject. InVisual c ++Folder, selectMFC ApplicationIcon.

    InNameBox, enterMFC01. Change the solution settingAdd to Solution. ClickOK.MFC Application WizardAppears.

    InMFC Application Wizard, Select application type. ChooseDialog based. Accept the remaining defaults and clickFinish. This will create an MFC application with an MFC dialog.

  2. Add a placeholder control to the MFC dialog box.

    ClickResource ViewTab. In resource view, double-click onIDD_MFC01_DIALOG. The dialog resource appears in resource editor.

    InToolbox, OpenDialog EditorList. drag a static text control to the dialog resource. the static text control will serve as a placeholder for. net Windows Forms control. resize it to approximately the size of the Windows Forms control.

    Change the ID of the static text controlIDC_CTRL1InPropertiesWindow and changeTabstopPropertyTrue.

  3. Configure the project.

    InSolution Explorer, Right-clickMFC01Project node, and selectPropertiesFrom the context menu.Property pagesDialog box appears.

    InProperty pagesDialog box, inConfiguration PropertiesTree control, selectGeneral, Then inProject defaultsSection, SetCommon Language Runtime supportToCommon Language Runtime support (/CLR). ClickOK.

  4. Add a reference to the. NET control.

    InSolution Explorer, Right-clickMFC01Project node and selectReferences. InProperty page, ClickAdd new reference, Select windowscontrollibrary1 (underProjectsTab), and clickOK. This adds a reference in the form of /Fu compiler option so that the program will compile; it also copies windowscontrollibrary1.dll intoMFC01Project directory so that the program will run.

  5. 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

  6. Add code to create the managed control.

    First, declare the managed control. In mfc01dlg. H, go to the declaration of the dic0class, 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 overrideCMFC01Dlg::DoDataExchangeGenerated by the MFC Application Wizard (notCAboutDlg::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);}

  7. Build and run the project.

    InSolution Explorer, Right-click mfc01 and selectSet as startup project.

    FromBuildMenu, clickBuild Solution.

    FromDebugMenu, clickStart without debugging. You will now see the MFC dialog box displaying the Windows form control.

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.