如何在MFC程序使用ActiveReports

发布时间:2016/01/08 00:01 发布者:frank.zhang

返回博客中心

本文主要介绍如何在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


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网