公司積累多年的 Delphi 報表引擎(ReportFram.ocx),在遷移至 .NET(WPF + OpenExpressApp)初期無法重寫,需跨語言復用。本文記錄將 Delphi ActiveX 封裝為 OCX,並在 WPF 中通過 WindowsFormsHost 宿主的完整步驟。
ActiveX Form,將報表引擎邏輯封裝其中。ReportFram.ocx 並註冊(regsvr32)。ReportFram.ocx。ReportFramX Control 拖入 Form。ReportFram.dll(RCW)與 AxReportFram.dll(ActiveX 封裝)。XAML:
<Grid>
<WindowsFormsHost Name="windowsFormsHost1" />
</Grid>
代碼後置:
public Window1()
{
InitializeComponent();
var rptFram = new AxReportFram.AxReportFramX(); // ActiveX 包裝控件
windowsFormsHost1.Child = rptFram; // 宿主接管
}
WPF 窗口中無縫嵌入 Delphi 報表引擎 UI,功能完整保留。
WindowsFormsHost 是 WPF 宿主 WinForms/ActiveX 控件的橋樑。aximp.exe 更直觀不易出錯。