本文主要介绍如何在MFC程序通过调用WPF来使用ActiveReports
1.创建WPF工程
2.创建报表
GrapeCity.ActiveReports.Document.PageDocument document1 = new GrapeCity.ActiveReports.Document.PageDocument(report1);
document1.LocateDataSource += new LocateDataSourceEventHandler(pageDocument_LocateDataSource);
viewer.LoadDocument(document1);
3.建立中间工程BridgeEx
public ref class CHostWPFWnd
{
public:
CHostWPFWnd(void){};
~CHostWPFWnd(void){};
protected:
!CHostWPFWnd(){};
public:
static MainWindow^ hostedWnd;
static HWND hWnd;
static WinSwitch^ hostedWndSwitch;
};
HWND GetHwnd(HWND hwnd = NULL);
HWND GetSwitchHwnd(HWND hwnd = NULL);
#using "../Debug/WpfActiveReports.dll"
void ShowWpfReports(int nType)
{
if(1 == nType) //Error
{
::GetHwnd();
if (CHostWPFWnd::hostedWnd)
{
CHostWPFWnd::hostedWnd->DirectShowWindow();
}
}
else if(0 == nType)
{
::GetSwitchHwnd();
if (CHostWPFWnd::hostedWndSwitch)
{
CHostWPFWnd::hostedWndSwitch->Show();
}
}
}
4.创建MFC程序
void CMainFrame::OnReport()
{
// TODO: 在此添加命令处理程序代码
if(NULL == m_hReportNew)
m_hReportNew = LoadLibrary("BridgeEx.dll");
OnShowErrorReport(0);
}
程序使用公共运行时编译的dll,使用托管c++调用.net的wpf窗体
在MFC中需要使用前台线程
WindowInteropHelper^ GetInteropHandler()
{
CHostWPFWnd::hostedWnd = gcnew MainWindow();
//CHostWPFWnd::hostedWnd->ClickEvent += gcnew WindowTest::ButtonClickHandler(Add);
WindowInteropHelper^ wih = gcnew WindowInteropHelper(CHostWPFWnd::hostedWnd);
return wih;
}
HWND GetHwnd(HWND hwnd)
{
//System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(new System.Func<IntPtr>(()=>IntPtr.Zero));
System::Object^ obj = System::Windows::Threading::Dispatcher::CurrentDispatcher->Invoke(
gcnew System::Func<WindowInteropHelper^>(&GetInteropHandler));
//CHostWPFWnd::hostedWnd = gcnew MainWindow();
////CHostWPFWnd::hostedWnd->ClickEvent += gcnew WindowTest::ButtonClickHandler(Add);
//WindowInteropHelper^ wih = gcnew WindowInteropHelper(CHostWPFWnd::hostedWnd);
WindowInteropHelper^ wih = static_cast<WindowInteropHelper^>(obj);
wih->Owner = IntPtr(hwnd);
CHostWPFWnd::hWnd = (HWND) wih->Handle.ToPointer();
return CHostWPFWnd::hWnd;
}
5.打开报表
通过调用一个Wpf窗口,然后通过这个窗体再调用有ActiveReports控件的窗体
6.直接打开报表
直接用/clr dll调用的ActiveReports控件所在的窗体
源码下载:
您在使用产品过程中有任何疑问,可以登录葡萄城开发者社区和经验丰富的技术工程师、ActiveReports开发人员交流:了解更多。
了解ActiveReports产品更多特性:
下载产品体验产品功能: