如何在MFC程序使用ActiveReports

本文主要介绍如何在MFC程序通过调用WPF来使用ActiveReports

发布于 2016/01/08 00:00

ActiveReports

本文主要介绍如何在MFC程序通过调用WPF来使用ActiveReports

1.创建WPF工程

2016-01-08_102027

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控件的窗体

2016-01-08_102541

2016-01-08_102605

 

6.直接打开报表

直接用/clr dll调用的ActiveReports控件所在的窗体

2016-01-08_102647

 

源码下载:

 

您在使用产品过程中有任何疑问,可以登录葡萄城开发者社区和经验丰富的技术工程师、ActiveReports开发人员交流:了解更多

了解ActiveReports产品更多特性:

/developer/activereports

下载产品体验产品功能:

/download/?pid=16

ActiveReports 报表控件| 下载试用

ActiveReports 是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForm / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求,作为专业的报表工具为全球超过 300,000 开发人员提供了全面的报表开发服务。

您对ActiveReports产品的任何技术问题,都有技术支持工程师提供1对1专业解答,点击此处即可发帖提问>>技术支持论坛

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态