简体   繁体   English

在C ++ / MFC应用程序中使用.NET?

[英]Use .NET in an C++/MFC applications?

The program we manage and develop is written in C++/MFC. 我们管理和开发的程序是用C ++ / MFC编写的。 We want to modernize it to .NET, but do not want to rewrite the entire application at once. 我们希望将其更新为.NET,但不希望立即重写整个应用程序。 Therefore, we are thinking about writing new parts of the program in .NET, while maintaining the rest of the program in C++/MFC. 因此,我们正在考虑在.NET中编写程序的新部分,同时在C ++ / MFC中维护程序的其余部分。

When we read about this on Internet it says a lot about calling unmanaged code from managed code. 当我们在Internet上阅读有关此内容时,它说了很多有关从托管代码中调用非托管代码的信息。 But we want to do it the other way. 但是我们想换一种方式。 We've found some information that it is possible, but it seem very tedious and cumbersome. 我们发现了一些可能的信息,但看起来非常乏味且繁琐。

Is that possible? 那可能吗? Is it a good solution? 这是一个好的解决方案吗? Are there better ways to incrementally modernize the program? 是否有更好的方法来使程序逐步现代化?

It's a common way to use C++/CLI to use the .NET parts from C++/MFC code. 这是使用C ++ / CLI来使用C ++ / MFC代码中的.NET部件的常用方法。 It isn't that hard when you get to know it. 知道它并不难。 .NET UI controls should have a methods to set the HWND of parent window, and the size of the control (not all of them have though). .NET UI控件应具有一种方法来设置父窗口的HWND以及控件的大小(尽管并非所有控件都具有)。 Another thing that many .NET components struggle with, is the obfuscation of the .NET assemblies that prevent using the controls from C++/CLI. 许多.NET组件所苦恼的另一件事是,.NET程序集的混淆性使得无法使用C ++ / CLI中的控件。

We are a .net chart manufacturer, and customers have used our products to modernize their apps. 我们是.net图表制造商,客户已使用我们的产品来更新其应用程序。 We've got documentation and example apps of how to use .NET charts from C++/CLI. 我们提供了有关如何使用C ++ / CLI中的.NET图表的文档和示例应用程序。

#using "Arction.LightningChartUltimate.dll"
using namespace Arction::LightningChartUltimate;

//"Global managed variables"  
ref class GlobalObjects {
public:
   static Arction::LightningChartUltimate::LightningChartUltimate^ chart; 
}; 

void CMainFrame::CreateChart(HWND hwndParent)
{

    GlobalObjects^ go = gcnew GlobalObjects();

    //Embeddable trial key is used here. 
    go->chart = gcnew LightningChartUltimate("licensekey"); 

    LightningChartUltimate^ chart = go->chart;

    //Disable repaints for every property change
    chart->BeginUpdate(); 

    //Set parent window by window handle
    chart->SetParentWindow((System::IntPtr) hwndParent); 

    //Remove existing series 
    chart->ViewXY->PointLineSeries->Clear(); 

    //Create new series 
    PointLineSeries^ series = gcnew PointLineSeries(chart->ViewXY, chart->ViewXY->XAxes[0], chart->ViewXY->YAxes[0]);
    const int PointCount = 10; 

    //Create SeriesPoint array
    array<SeriesPoint> ^ data =
        gcnew array<SeriesPoint>(PointCount);

    //Fill the array
    double yValues[PointCount] = {0.0, 5.0, 4.0, 3.0, 8.0, 10.0, 9.0, 8.0, 3.0, 2.0};
    for(int i=0; i<PointCount; i++)
    {
        data[i].X = i+1; 
        data[i].Y = yValues[i]; 
    }

    //Add the points to series
    series->AddPoints(data,false);

    //Add the series itself into chart's PointLineSeries collection
    chart->ViewXY->PointLineSeries->Add(series); 

    //Fit the axis ranges to data assigned
    chart->ViewXY->FitView();

    //Allow repaints, update chart
    chart->EndUpdate();  

} 

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
    __super::OnSize(nType, cx, cy);

    RECT rectStatusBar;
    if (m_wndStatusBar.m_hWnd)
        m_wndStatusBar.GetClientRect(&rectStatusBar);

    //Set the new size to LightningChart object, with SetBounds method call
    GlobalObjects^ go = gcnew GlobalObjects();
    if (go)
    {
        LightningChartUltimate^ chart = go->chart;
        if (chart)
            chart->SetBounds(2, 2, cx - 4, cy - rectStatusBar.bottom - 2);
    }
}

It isn't harder than that, pretty much the same amount of code is needed than from C#. 这并不困难,所需的代码量几乎与C#相同。

Another approach is to make new DLLs with in C# (WinForms or WPF), and export a simple API for the C++ app, which minimizes the C++/CLI usage in your unmanaged side. 另一种方法是使用C#(WinForms或WPF)制作新的DLL,并为C ++应用程序导出一个简单的API,这可以最大程度地减少非托管方面对C ++ / CLI的使用。

(I'm the CTO of LightningChart components.) (我是LightningChart组件的CTO。)

The easiest way for your to archive this would be using COM+ to host your C# and then from C++ add those COM+ as references. 存档的最简单方法是使用COM +托管C#,然后从C ++添加这些COM +作为引用。 It is a simple procedure which also adds to a few benefits like code encapsulation. 这是一个简单的过程,还增加了一些好处,例如代码封装。

http://my.execpc.com/~gopalan/dotnet/complus/complus.net_accountmanager.html http://my.execpc.com/~gopalan/dotnet/complus/complus.net_accountmanager.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM