DUIlib使用Fastreport--报表简单使用

  fastreport是一个简单优秀的报表,fastreport更多是和delphi联合使用预览和打印数据的。我在开始使用duilib做项目时,打印和数据预览都是自己绘制的,这样不仅绘制麻烦费事费事,而且客户需求是多变的,自己绘制不是那么的灵活。后面我觉得,可以使用报表等工具结合duilib进行显示或者打印,这里我使用了ocx调用fastreport 的方式。

   首先我们从网上下载fastreport进行安装,这里提供网盘的下载,如果链接失效请联系本人。链接:http://pan.baidu.com/s/1bp4ZrCr 密码:3y5j。安装好后的fastreport有一些例子,感兴趣的同学可以看看。

  以上工作完成后,就可以编写我们的代码了,duilib已经封装好了对ocx的支持,xml的关键代码如下:

      <ActiveX name="FastReport" clsid="{9207E7EB-4CE3-40E1-8A5A-77C43DA9F503}" mouse="false" >
      </ActiveX>

接下来我们编写一个窗口来显示这个ocx

 1 class CFrameWindowWnd
 2     :public WindowImplBase
 3 {
 4 public:
 5     CFrameWindowWnd();
 6     ~CFrameWindowWnd();
 7 
 8 protected:
 9     CDuiString GetSkinFolder()override;
10     CDuiString GetSkinFile()override;
11     LPCTSTR GetWindowClassName(void) const override;
12     void OnFinalMessage(HWND hWnd)override;
13     void Notify(TNotifyUI& msg);
14     void OnClick(TNotifyUI& msg);
15 
16 protected:
17 
18 private:
19     IfrxActivePreviewForm* m_pFastReportView;
20     CActiveXUI* m_pActiveX;
21     IfrxReportPtr m_pReport;
22 
23     //Control
24     CButtonUI *m_pLoadReportBtn;
25 };
  1 #include "stdafx.h"
  2 #include "FrameWindowWnd.h"
  3 
  4 CFrameWindowWnd::CFrameWindowWnd()
  5 :m_pFastReportView(nullptr),
  6 m_pActiveX(nullptr),
  7 m_pLoadReportBtn(nullptr),
  8 m_pReport(nullptr)
  9 {
 10     {
 11         IfrxReportPtr pReport(__uuidof(TfrxReport));
 12         m_pReport = pReport;
 13     }
 14 }
 15 
 16 
 17 CFrameWindowWnd::~CFrameWindowWnd()
 18 {
 19     m_pReport->Release();
 20 }
 21 
 22 DuiLib::CDuiString CFrameWindowWnd::GetSkinFolder()
 23 {
 24     return _T("");
 25 }
 26 
 27 DuiLib::CDuiString CFrameWindowWnd::GetSkinFile()
 28 {
 29     return _T("FrameWindowWnd.xml");
 30 }
 31 
 32 LPCTSTR CFrameWindowWnd::GetWindowClassName(void) const
 33 {
 34     return _T("FrameWindowWnd");
 35 }
 36 
 37 //"87caf5e4-3add-4abd-baf5-76f3ee604c3d"
 38 static const GUID IID_IZ =
 39 { 0x87caf5e4, 0x3add, 0x4abd, { 0xba, 0xf5, 0x76, 0xf3, 0xee, 0x60, 0x4c, 0x3d } };
 40 
 41 void CFrameWindowWnd::Notify(TNotifyUI& msg)
 42 {
 43     if (msg.sType == _T("showactivex"))
 44     {
 45         if (msg.pSender->GetName() != _T("FastReport")) return;
 46         m_pActiveX = static_cast<CActiveXUI*>(msg.pSender);
 47         if (m_pActiveX != nullptr)
 48         {
 49             m_pActiveX->GetControl(IID_IZ, (void**)&m_pFastReportView);
 50 
 51             if ( m_pFastReportView != nullptr )
 52             {
 53                  
 54 //                 DUITRACE("%d", m_pFastReportView->Report);
 55                  DUITRACE("test");
 56 //                 m_pFastReportView->Init();
 57                 frxPreviewButtons btns = m_pReport->GetPreviewOptions()->GetButtons();
 58 
 59                 btns = frxPreviewButtons(btns^frxPreviewButtons::pb_NoClose);
 60 
 61                  m_pReport->GetPreviewOptions()->PutButtons(btns);
 62 
 63                 m_pFastReportView->PutReport(m_pReport);
 64                 m_pFastReportView->Init();
 65              }
 66         }
 67     }
 68     else if (msg.sType == DUI_MSGTYPE_WINDOWINIT)
 69     {
 70         m_pLoadReportBtn = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("LoadReport")));
 71         DUITRACE(m_PaintManager.FindControl(_T("LoadReport"))->GetName());
 72         DUITRACE(m_PaintManager.FindControl(_T("LoadReport"))->GetClass());
 73         assert(m_pLoadReportBtn != nullptr);
 74     }
 75 
 76     __super::Notify(msg);
 77 }
 78 
 79 void CFrameWindowWnd::OnFinalMessage(HWND hWnd)
 80 {
 81     WindowImplBase::OnFinalMessage(hWnd);
 82     PostQuitMessage(0);
 83     delete this;
 84 }
 85 
 86 void CFrameWindowWnd::OnClick(TNotifyUI& msg)
 87 {
 88     if (msg.pSender == m_pLoadReportBtn)
 89     {
 90         //m_pFastReportView->Lock();
 91 #if 0
 92         CDuiString szReportFileName = CPaintManagerUI::GetInstancePath() + _T("01.Simple list.fr3");
 93         m_pReport->LoadReportFromFile(szReportFileName.GetData());
 94         m_pReport->PrepareReport(true);
 95         m_pReport->ShowReport();
 96 #else
 97 
 98         m_pFastReportView->Lock(); //Workspace.Lock();
 99         try
100         {
101             CDuiString szReportFileName = CPaintManagerUI::GetInstancePath() + _T("01.Simple list.fr3");
102             m_pReport->LoadReportFromFile(szReportFileName.GetData());
103             m_pReport->PrepareReport(true);
104         }
105         catch (...)
106         {
107         }
108         m_pFastReportView->Unlock(); // Workspace.Unlock();
109         //m_pFastReportView->Update();
110 #endif
111         //m_pFastReportView->Unlock(); // Workspace.Unlock();
112     }
113     __super::OnClick(msg);
114 }
View Code

好了,这是一个简单的页面。这里的报表是我随便从fastreport的安装目录下面复制过来的。编译好来看看我们的成果。

效果还是不错吧?!至于Duilib向fastreport传参数,绑定变量大家可以研究fastreport的例子。

部署的话,我们不用安装fastreport,可以直接拷贝fastreport3.dll到客户机上然后regsvr32一下就好了,是不是很简单?

 代码下载

转载请注明出处http://www.cnblogs.com/fyluyg/p/5748626.html

posted on 2016-08-08 11:54  漂泊的浮萍  阅读(1909)  评论(14编辑  收藏  举报