[英]Use .NET in an C++/MFC applications?
我們管理和開發的程序是用C ++ / MFC編寫的。 我們希望將其更新為.NET,但不希望立即重寫整個應用程序。 因此,我們正在考慮在.NET中編寫程序的新部分,同時在C ++ / MFC中維護程序的其余部分。
當我們在Internet上閱讀有關此內容時,它說了很多有關從托管代碼中調用非托管代碼的信息。 但是我們想換一種方式。 我們發現了一些可能的信息,但看起來非常乏味且繁瑣。
那可能嗎? 這是一個好的解決方案嗎? 是否有更好的方法來使程序逐步現代化?
這是使用C ++ / CLI來使用C ++ / MFC代碼中的.NET部件的常用方法。 知道它並不難。 .NET UI控件應具有一種方法來設置父窗口的HWND以及控件的大小(盡管並非所有控件都具有)。 許多.NET組件所苦惱的另一件事是,.NET程序集的混淆性使得無法使用C ++ / CLI中的控件。
我們是.net圖表制造商,客戶已使用我們的產品來更新其應用程序。 我們提供了有關如何使用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);
}
}
這並不困難,所需的代碼量幾乎與C#相同。
另一種方法是使用C#(WinForms或WPF)制作新的DLL,並為C ++應用程序導出一個簡單的API,這可以最大程度地減少非托管方面對C ++ / CLI的使用。
(我是LightningChart組件的CTO。)
存檔的最簡單方法是使用COM +托管C#,然后從C ++添加這些COM +作為引用。 這是一個簡單的過程,還增加了一些好處,例如代碼封裝。
http://my.execpc.com/~gopalan/dotnet/complus/complus.net_accountmanager.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.