VC2015調用Grid++report報表控件
阿新 • • 發佈:2018-05-21
Grid++report組件Grid++report這是一個國產報表控件,從2.x就開始接觸,基本所有的學習資源,來自於自帶的文檔和例子。能學多少靠摸索。整體功能還是不錯的。他提供了3個控件,一個組件。在VC2015如果不想用控件,只想調用組件實現打印的功能,怎麽辦呢?步驟如下:
(1)在afxstd.h文件中包含頭文件如下:
#include <atlbase.h> extern CComModule _Module; #include <atlcom.h>
(2)在程序APP文件中,聲明如下:
CComModule _Module;//全局變量 BOOL CMFCApplication1App::InitInstance() {//初始化報表COM組件 HRESULT hRes = ::CoInitialize(NULL); ATLASSERT(SUCCEEDED(hRes)); _Module.Init(0, AfxGetInstanceHandle()); //...... } int CMFCApplication1App::ExitInstance() {//重寫虛函數 _Module.Term(); ::CoUninitialize(); return CWinApp::ExitInstance(); }
(3)在對話框的頭文件中,加入如下:
#include "GetPath.h" #include "GRImport.h"
當然需要先將Grid++report目錄下的Utility文件夾復制到工程目錄中,並在“項目屬性->VC++目錄->包含目錄”添加Utility文件夾。
(4)在對話框類中添加成員變量:IGridppReportPtr m_pGridppReport;
BOOL CMFCApplication1Dlg::OnInitDialog() { //創建報表主對象 m_pGridppReport.CreateInstance(__uuidof(GridppReport)); ATLASSERT(m_pGridppReport != NULL); //加載模板文件 //從文件中載入報表模板數據到報表主對象 CString FileName = GetReportTemplatePath(_T("標準過磅單1.grf")); m_pGridppReport->LoadFromFile((LPCTSTR)FileName); //第(7)步還有代碼 }
void CMFCApplication1Dlg::OnDestroy() { CDialogEx::OnDestroy(); //釋放主報表對象 m_pGridppReport.Release(); } void CMFCApplication1Dlg::OnBnClickedButton1() { //直接打印報表 m_pGridppReport->Print(TRUE); } void CMFCApplication1Dlg::OnBnClickedButton2() { // 顯示預覽窗口 m_pGridppReport->Title = _T("標準過磅單1"); m_pGridppReport->PrintPreview(TRUE); }
(5)同時將導出庫復制到工程路徑下,即gregn.tlb 和 grdes.tlb
(6)要動態修改報表參數,可以新建一個類,繼承報表事件處理接口。
//Scale3DCReportEvent.h頭文件 #pragma once #include "GridppReportEventImpl.h" class CScale3DCReportEvent :public CGridppReportEventImpl { public: CScale3DCReportEvent(); ~CScale3DCReportEvent(); virtual void Initialize(void); IGridppReportPtr m_pGridppReport; };
//Scale3DCReportEvent.cpp文件 #include "stdafx.h" #include "Scale3DCReportEvent.h" #include "GridppReportEventImpl.c" CScale3DCReportEvent::CScale3DCReportEvent() { } CScale3DCReportEvent::~CScale3DCReportEvent() { } void CScale3DCReportEvent::Initialize(void) { m_pGridppReport->ParameterByName(_T("車號"))->AsString =_T("獵豹太空梭0X29"); }
(7)在相應的對話框頭文件中包含:
#include "Scale3DCReportEvent.h"//事件包裝類
#include "GetPath.h" //上面步驟已包含過
#include "GRImport.h"
在窗口類中聲明成員變量:
//報表事件代理指針
CGridppReportEventImpl *m_pReportEvents;
在窗口的OnInitDialog()中添加如下代碼:
//創建事件響應對象 CComObject<CScale3DCReportEvent> *pEvent; CComObject<CScale3DCReportEvent>::CreateInstance(&pEvent); m_pReportEvents = pEvent; m_pReportEvents->AddRef(); pEvent->m_pGridppReport = m_pGridppReport;//事件代理指針 HRESULT hr = m_pReportEvents->DispEventAdvise(m_pGridppReport, &__uuidof(_IGridppReportEvents)); ATLASSERT( SUCCEEDED(hr) );
(8)在ON_WM_DESTROY消息中繼續添加代碼:
//釋放事件代理 if (m_pReportEvents != NULL) { HRESULT hr = m_pReportEvents->DispEventUnadvise(m_pGridppReport, &__uuidof(_IGridppReportEvents)); m_pReportEvents->Release(); m_pReportEvents = NULL; ATLASSERT(SUCCEEDED(hr)); }
VC2015調用Grid++report報表控件