MFC下 报表打印程序的实现

VC 6.0下实现的 报表打印类 对数据制定打印位置 位置调整 打印预览 以及批量打印

适用于 罚单、发票等批量打印要求

以下为程序代码:

// ReportPrint.h: interface for the ReportPrint class.
//
//

#if !defined(AFX_REPORTPRINT_H__7A35BA1E_B178_4C1B_8970_5226028695DB__INCLUDED_)
#define AFX_REPORTPRINT_H__7A35BA1E_B178_4C1B_8970_5226028695DB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#define POINT_COUNT 2
//打印信息结构体
typedef struct tag_PRINTINFO
{
CString text[POINT_COUNT];
}PRINTINFO;

class ReportPrint
{
public:
BOOL m_bEditMode;
void SetTextFont(char* height,char* facename);
void PrintPreview();
void Print(BOOL bDirect);
void SetPrintInfo(PRINTINFO printInfo[],int size);
void SetPaperSize(int x,int y);
void SetPaperSize(CSize paperSize);
ReportPrint();
virtual ~ReportPrint();
private:
PRINTINFO* m_pPrintInfo;

CSize m_paperSize;//打印纸张型号默认 A4 297*210
int m_nCurPage;//当前页
int m_nMaxPage;//最大页
CString m_csHeight;
CString m_csFaceName;
CPoint m_printPoint[POINT_COUNT];//打印坐标数组
};

#endif // !defined(AFX_REPORTPRINT_H__7A35BA1E_B178_4C1B_8970_5226028695DB__INCLUDED_)
/
// CDlgReportPrintPreview dialog

class CDlgReportPrintPreview : public CDialog
{
// Construction
public:
BOOL m_bEditMode;
CDlgReportPrintPreview(CWnd* pParent = NULL); // standard constructor
CDlgReportPrintPreview::CDlgReportPrintPreview(CString& csHeight,CString& csFaceName, CSize paperSize, PRINTINFO printInfo[], int nMaxPage, CPoint* pPrintPoint, int nPrintCount,ReportPrint* parent = NULL);
// Dialog Data
//{{AFX_DATA(CDlgReportPrintPreview)
enum { IDD = IDD_DLG_PRINT_PREVIEW };
CComboBoxm_ctrlNodes;
intm_nCurPage;
intm_nPosX;
intm_nPosY;
//}}AFX_DATA


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDlgReportPrintPreview)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CDlgReportPrintPreview)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg void OnBnPre();
afx_msg void OnBnNext();
virtual void OnOK();
afx_msg void OnBnSetPos();
afx_msg void OnSelchangeComboNode();
afx_msg void OnChangeEditY();
afx_msg void OnChangeEditX();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
void DrawPrintPreview(CDC* pDC,CRect* previewRect);

CRect m_previewRect;//打印预览矩形默认 297*210
CSize m_paperSize;
PRINTINFO* m_pPrintInfo;
int m_nMaxPage;//最大页
double m_nRato;//预览缩放比率
CPoint* m_pPrintPoint;//打印坐标数组
int m_nPrintCount;
ReportPrint* m_pParent;
CString m_csHeight;
CString m_csFaceName;
};

// ReportPrint.cpp: implementation of the ReportPrint class.
//
//

#include "stdafx.h"
#include "MyPrint.h"
#include "ReportPrint.h"
#include <winspool.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

ReportPrint::ReportPrint()
{
m_printPoint[0].x = 20;
m_printPoint[0].y = 20;
m_printPoint[1].x = 20;
m_printPoint[1].y = 200;
}

ReportPrint::~ReportPrint()
{

}
//设置打印字体 默认 宋体15号
void ReportPrint::SetTextFont(char* height,char* faceName)
{

m_csHeight = height;
m_csFaceName = faceName;
}

//设置打印纸张的大小
void ReportPrint::SetPaperSize(CSize paperSize)
{
m_paperSize = paperSize;
}
//设置打印纸张的大小 重载
void ReportPrint::SetPaperSize(int x, int y)
{
m_paperSize.cx = x;
m_paperSize.cy = y;
}
//设置打印数据 同时设置批量打印的数量
void ReportPrint::SetPrintInfo(PRINTINFO printInfo[], int size)
{
m_pPrintInfo = printInfo;
m_nMaxPage = size;
}
//打印
void ReportPrint::Print(BOOL bDirect)
{
DWORD dwflags=PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_SELECTION | PD_HIDEPRINTTOFILE;
CPrintDialog dlg(0);//(bDirect,dwflags,NULL);
//dlg.GetDefaults();

//if (dev = dlg.GetDevMode());
if(dlg.DoModal()==IDOK)
{

CDC dc;

//定义打印模式信息
LPDEVMODE dev = dlg.GetDevMode();


dev->dmPaperSize = DMPAPER_USER;
dev->dmPaperLength = m_paperSize.cy * 10;
dev->dmPaperWidth = m_paperSize.cx * 10;
dev->dmFields = DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH | dev->dmFields;
dev->dmFields = dev->dmFields | DMBIN_MANUAL;
dev->dmDefaultSource = DMBIN_MANUAL;

strcpy((char*)dev->dmFormName,"自定义");
dc.Attach(dlg.CreatePrinterDC());//打印机DC构造完成
/*
DOCINFO di;
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Printing Test ";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
*/
dc.StartDoc("print");

CFont font;
font.CreatePointFont(atoi(m_csHeight)*10,m_csFaceName,&dc);

dc.SelectObject(&font);//设置用户配置字体

for (int i=0;i<m_nMaxPage;i++)
{
dc.StartPage();
for (int j=0;j<POINT_COUNT;j++)
{
dc.TextOut(m_printPoint[j].x*10,m_printPoint[j].y*10,m_pPrintInfo[i].text[j]);
}
dc.EndPage();
}

dc.EndDoc();
font.DeleteObject();
dc.DeleteDC();
}


}
//打印预览
void ReportPrint::PrintPreview()
{
CDlgReportPrintPreview dlg(m_csHeight,m_csHeight,m_paperSize,m_pPrintInfo,m_nMaxPage,m_printPoint,POINT_COUNT,this);
dlg.m_bEditMode = m_bEditMode;
dlg.DoModal();
}

/
// CDlgReportPrintPreview dialog


CDlgReportPrintPreview::CDlgReportPrintPreview(CWnd* pParent /*=NULL*/)
: CDialog(CDlgReportPrintPreview::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgReportPrintPreview)
m_nCurPage = 0;
m_nPosX = 0;
m_nPosY = 0;
//}}AFX_DATA_INIT
}

CDlgReportPrintPreview::CDlgReportPrintPreview(CString& csHeight,CString& csFaceName, CSize paperSize, PRINTINFO printInfo[], int nMaxPage, CPoint* pPrintPoint, int nPrintCount,ReportPrint* parent )
: CDialog(CDlgReportPrintPreview::IDD, NULL)
{
//初始化字体
m_csHeight = csHeight;
m_csFaceName = csFaceName;
//初始化纸张大小
m_paperSize = paperSize;
//初始化打印信息
m_pPrintInfo = printInfo;
//初始化最大打印页数
m_nMaxPage = nMaxPage;
//初始化打印坐标
m_pPrintPoint = pPrintPoint;
//初始化打印坐标个数
m_nPrintCount = nPrintCount;
//父类指针
m_pParent = parent;

m_previewRect.bottom = m_previewRect.top = m_previewRect.left = m_previewRect.right =0;
m_nCurPage=1;

m_bEditMode = FALSE;


}
void CDlgReportPrintPreview::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgReportPrintPreview)
DDX_Control(pDX, IDC_COMBO_NODE, m_ctrlNodes);
DDX_Text(pDX, IDC_EDIT_CUR_PAGE, m_nCurPage);
DDX_Text(pDX, IDC_EDIT_X, m_nPosX);
DDX_Text(pDX, IDC_EDIT_Y, m_nPosY);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgReportPrintPreview, CDialog)
//{{AFX_MSG_MAP(CDlgReportPrintPreview)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_BN_PRE, OnBnPre)
ON_BN_CLICKED(IDC_BN_NEXT, OnBnNext)
ON_BN_CLICKED(IDC_BN_SET_POS, OnBnSetPos)
ON_CBN_SELCHANGE(IDC_COMBO_NODE, OnSelchangeComboNode)
ON_EN_CHANGE(IDC_EDIT_Y, OnChangeEditY)
ON_EN_CHANGE(IDC_EDIT_X, OnChangeEditX)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CDlgReportPrintPreview message handlers

BOOL CDlgReportPrintPreview::OnInitDialog()
{
CDialog::OnInitDialog();

CString str;
int i=0;
for (;i<POINT_COUNT;i++)
{

str.Format(" [%d] ",i);
m_ctrlNodes.AddString(str);
}
m_ctrlNodes.SelectString(i,str);

m_ctrlNodes.SetCurSel(0);
m_nPosX = m_pPrintPoint[0].x;
m_nPosY = m_pPrintPoint[0].y;
CString csPosX;
csPosX.Format("%d",m_nPosX);
CString csPosY;
csPosY.Format("%d",m_nPosY);
SetDlgItemText(IDC_EDIT_X,csPosX);
SetDlgItemText(IDC_EDIT_Y,csPosY);
UpdateData(TRUE);


if (m_bEditMode == FALSE)
{
GetDlgItem(IDC_STATIC_NODE)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_POS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_BN_SET_POS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_COMBO_NODE)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_EDIT_X)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_EDIT_Y)->ShowWindow(SW_HIDE);
}


return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgReportPrintPreview::OnPaint()
{

//CPaintDC pDC(this->GetDlgItem(IDD_DLG_PRINT_PREVIEW));
CDialog::OnPaint();

CDC* pDC;
pDC = GetDC();
GetClientRect(&m_previewRect);//获得客户区大小

m_previewRect.left +=40;
m_previewRect.right-=40;
m_previewRect.top +=70;
m_previewRect.bottom -=40;

m_nRato=float(m_previewRect.right-m_previewRect.left)/m_paperSize.cx;//预览窗口的宽度除以纸张的宽度 得到预览缩放比例
m_previewRect.bottom =m_previewRect.top+int(m_paperSize.cy*m_nRato);//用缩放比例调整预览纸张的长度 保证预览纸张的高宽比
DrawPrintPreview(pDC,&m_previewRect);
}

void CDlgReportPrintPreview::DrawPrintPreview(CDC *pDC, CRect *previewRect)
{
CFont font,*pOldFont;
CPen pen,*pOldPen;


GetDlgItem(IDC_STATIC_MAP)->ShowWindow(SW_HIDE);

pen.CreatePen(PS_SOLID,2,RGB(255,0,0));//创建画笔

font.CreatePointFont(int(atoi(m_csHeight)*10/m_nRato),m_csFaceName,pDC);

pOldFont=pDC->SelectObject(&font);//将字体选入
pOldPen=pDC->SelectObject(&pen);//将画笔选入
pDC->SetBkMode(OPAQUE);//设置位图背景模式为不透明
pDC->SetBkColor(RGB(255,255,255));//设置位图背景颜色为白色

//填充位图
pDC->FillSolidRect(previewRect->left-1,previewRect->top-1,previewRect->Width()+2,previewRect->Height()+2,RGB(25,25,25));
pDC->FillSolidRect(previewRect->left,previewRect->top,previewRect->Width(),previewRect->Height(),RGB(255,255,255));

//设置文字显示颜色
pDC->SetTextColor(RGB(0,0,0));

//打印预览字体
for (int j=0;j<POINT_COUNT;j++)
{
CString str;
str.Format("%s[%d]",m_pPrintInfo[m_nCurPage-1].text[j],j+1);
pDC->TextOut(previewRect->left+m_pPrintPoint[j].x,previewRect->top+m_pPrintPoint[j].y,str);
}

font.DeleteObject();
pen.DeleteObject ();
}

void CDlgReportPrintPreview::OnBnPre()
{
if (m_nCurPage == 1)
{
AfxMessageBox("已经是第一页了");
}
else
{
m_nCurPage--;
UpdateData(FALSE);
Invalidate(FALSE);
}

}

void CDlgReportPrintPreview::OnBnNext()
{
if (m_nCurPage == m_nMaxPage)
{
AfxMessageBox("已经是最后一页了");
}
else
{
m_nCurPage++;
UpdateData(FALSE);
Invalidate(FALSE);
}
}

void CDlgReportPrintPreview::OnOK()
{
m_pParent->Print(FALSE);

//CDialog::OnOK();
}


void CDlgReportPrintPreview::OnBnSetPos()
{
UpdateData(FALSE);
int index = m_ctrlNodes.GetCurSel();
((m_pPrintPoint[index])).x = (GetDlgItemInt(IDC_EDIT_X));
((m_pPrintPoint[index])).y = (GetDlgItemInt(IDC_EDIT_Y));
int i = m_pPrintPoint[m_ctrlNodes.GetCurSel()].x;
int j = m_pPrintPoint[m_ctrlNodes.GetCurSel()].y;
UpdateData(TRUE);
Invalidate(FALSE);
}

void CDlgReportPrintPreview::OnSelchangeComboNode()
{
int index = m_ctrlNodes.GetCurSel();
if (index == -1)
{
index =0;
m_ctrlNodes.SetCurSel(0);
}
m_nPosX = m_pPrintPoint[index].x;
m_nPosY = m_pPrintPoint[index].y;
CString csPosX;
csPosX.Format("%d",m_nPosX);
CString csPosY;
csPosY.Format("%d",m_nPosY);
SetDlgItemText(IDC_EDIT_X,csPosX);
SetDlgItemText(IDC_EDIT_Y,csPosY);
UpdateData(TRUE);
}

void CDlgReportPrintPreview::OnChangeEditX()
{
m_nPosX = GetDlgItemInt(IDC_EDIT_X);
}

void CDlgReportPrintPreview::OnChangeEditY()
{
m_nPosY = GetDlgItemInt(IDC_EDIT_Y);
}


打印预览对话框


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值