Grid++report这是一个国产报表控件,从2.x就开始接触,基本所有的学习资源,来自于自带的文档和例子。能学多少靠摸索。整体功能还是不错的。他提供了3个控件,一个组件。在VC2015如果不想用控件,只想调用组件实现打印的功能,怎么办呢?步骤如下:
(1)在afxstd.h文件中包含头文件如下:
//#include<afxtempl.h>手册上说加,实际这一句可以不加,VC2015已经默认包含了 #include <atlbase.h> //GridReport 组件头文件 extern CComModule _Module;//声明变量
(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); }
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); }