// PreviewDemoDlg.cpp : implementation file
// Download by http://www.codefans.net
#include "stdafx.h"
#include "PreviewDemo.h"
#include "PreviewDemoDlg.h"
#include "PreParent.h" //打印预览父窗口
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPreviewDemoDlg dialog
CPreviewDemoDlg::CPreviewDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPreviewDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPreviewDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPreviewDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPreviewDemoDlg)
DDX_Control(pDX, IDC_LIST1, CLtv);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPreviewDemoDlg, CDialog)
//{{AFX_MSG_MAP(CPreviewDemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(BTN_PRINT, OnPrint)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPreviewDemoDlg message handlers
BOOL CPreviewDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CLtv.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
int nItem = 0;
CLtv.InsertColumn(nItem++, "学号", LVCFMT_LEFT, 50);
CLtv.InsertColumn(nItem++, "姓名", LVCFMT_LEFT, 60);
CLtv.InsertColumn(nItem++, "姓别", LVCFMT_LEFT, 50);
CLtv.InsertColumn(nItem++, "专业", LVCFMT_LEFT, 70);
CLtv.InsertColumn(nItem++, "年级", LVCFMT_LEFT, 70);
CLtv.InsertColumn(nItem++, "班级", LVCFMT_LEFT, 70);
for(int i=0; i<100; i++)
{
CString csTemp;
csTemp.Format("学号%i", i);
nItem = CLtv.InsertItem(0xFFFFFF, csTemp);
csTemp.Format("姓名%i", i);
int nSub = 1;
CLtv.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0);
csTemp.Format("男");
CLtv.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0);
csTemp.Format("专业%i", i);
CLtv.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0);
csTemp.Format("年级%i", i);
CLtv.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0);
csTemp.Format("班级%i", i);
CLtv.SetItem(nItem, nSub++, 1, csTemp, NULL, 0, 0, 0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPreviewDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPreviewDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPreviewDemoDlg::OnPrint()
{
if(CLtv.GetItemCount()<= 0)
return;
PRNINFO PrnInfo = {0};
PrnInfo.hListView = CLtv.m_hWnd;
PrnInfo.hWnd = this->m_hWnd;
PrnInfo.IsPrint = FALSE;
PrnInfo.nCurPage = 1;
PrnInfo.nMaxLine = CLtv.GetItemCount();
CPreParent DlgPreView;
DlgPreView.SetCallBackFun(DrawInfo, PrnInfo);
DlgPreView.DoModal();
}
void CPreviewDemoDlg::DrawInfo(CDC &memDC, PRNINFO PrnInfo)
{
if(memDC.m_hDC == NULL)
return;
int nCurPage = PrnInfo.nCurPage; //当前页
BOOL IsPrint = PrnInfo.IsPrint; //是否打印
int nMaxPage = PrnInfo.nCountPage; //最大页码
HWND hWnd = PrnInfo.hWnd;
HWND hList = PrnInfo.hListView;
CString csLFinality, csRFinality;
CTime time;
time=CTime::GetCurrentTime();
csLFinality = time.Format("报表日期:%Y-%m-%d");
csRFinality.Format("第 %i 页/共 %i 页", nCurPage, nMaxPage);
TCHAR szTitle[] = TEXT("学生花名册");
CRect rc, rt1, rt2, rt3, rt4, rt5, rt6;
CPen *hPenOld;
CPen cPen;
CFont TitleFont, DetailFont, *oldfont;
//标题字体
TitleFont.CreateFont(-MulDiv(14,memDC.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("黑体"));
//细节字体
DetailFont.CreateFont(-MulDiv(10,memDC.GetDeviceCaps(LOGPIXELSY),72),
0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH|FF_SWISS,_T("宋体"));
//粗笔
cPen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
int xP = GetDeviceCaps(memDC.m_hDC, LOGPIXELSX); //x方向每英寸像素点数
int yP = GetDeviceCaps(memDC.m_hDC, LOGPIXELSY); //y方向每英寸像素点数
DOUBLE xPix = (DOUBLE)xP*10/254; //每 mm 宽度的像素
DOUBLE yPix = (DOUBLE)yP*10/254; //每 mm 高度的像素
DOUBLE fAdd = 7*yPix; //每格递增量
DOUBLE nTop = 25*yPix; //第一页最上线
int iStart = 0; //从第几行开始读取
DOUBLE nBottom = nTop+B5_ONELINE*fAdd;
if(nCurPage != 1)
nTop = 25*yPix-fAdd; //非第一页最上线
if(nCurPage == 2)
iStart = B5_ONELINE;
if(nCurPage>2)
iStart = B5_ONELINE+(nCurPage - 2)*B5_OTHERLINE;
DOUBLE nLeft = 20*xPix; //最左线
DOUBLE nRight = xPix*(B5_W-20); //最右线
DOUBLE nTextAdd = 1.5*xPix;
if(IsPrint)
{
//真正打印部分
static DOCINFO di = {sizeof (DOCINFO), szTitle} ;
//开始文档打印
if(memDC.StartDoc(&di)<0)
{
::MessageBox(hWnd, "连接到打印机化败!", "错误", MB_ICONSTOP);
}
else
{
iStart = 0;
nTop = 25*yPix; //第一页最上线
for(int iTotalPages = 1; iTotalPages<=nMaxPage; iTotalPages++)
{
int nCurPage = iTotalPages;
csRFinality.Format("第 %i 页/共 %i 页", nCurPage, nMaxPage);
time=CTime::GetCurrentTime();
csLFinality = time.Format("报表日期:%Y-%m-%d");
if(nCurPage != 1)
nTop = 25*yPix-fAdd; //非第一页最上线
if(nCurPage == 2)
iStart = B5_ONELINE;
if(nCurPage>2)
iStart = B5_ONELINE+(nCurPage - 2)*B5_OTHERLINE;
//开始页
if(memDC.StartPage() < 0)
{
::MessageBox(hWnd, _T("打印失败!"), "错误", MB_ICONSTOP);
memDC.AbortDoc();
return;
}
else
{
//打印
//标题
oldfont = memDC.SelectObject(&TitleFont);
int nItem = B5_OTHERLINE;
if(nCurPage == 1)
{
nItem = B5_ONELINE;
rc.SetRect(0, yPix*10, B5_W*xPix, yPix*20);
memDC.DrawText(szTitle, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
//细节
memDC.SelectObject(&DetailFont);
rc.SetRect(nLeft, nTop, nRight, nTop+fAdd);
//上横线
memDC.MoveTo(rc.left, rc.top);
memDC.LineTo(rc.right, rc.top);
rt1.SetRect(nLeft, nTop, nLeft+20*xPix, nTop+fAdd); //学号
rt2.SetRect(rt1.right, rt1.top, rt1.right + 25*xPix, rt1.bottom); //姓名
rt3.SetRect(rt2.right, rt1.top, rt2.right + 10*xPix, rt1.bottom); //性别
rt4.SetRect(rt3.right, rt1.top, rt3.right + 25*xPix, rt1.bottom); //专业
rt5.SetRect(rt4.right, rt1.top, rt4.right + 25*xPix, rt1.bottom); //年级
rt6.SetRect(rt5.right, rt1.top, rc.right, rt1.bottom); //班级
memDC.DrawText("学 号", &rt1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
memDC.DrawText("姓 名", &rt2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
memDC.DrawText("性别", &rt3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
memDC.DrawText("专 业", &rt4, DT_CENTER | DT_
没有合适的资源?快使用搜索试试~ 我知道了~
vc的手动花HDC,打印功能
共21个文件
h:7个
cpp:6个
ico:2个
2星 需积分: 9 14 下载量 38 浏览量
2010-04-22
16:12:52
上传
评论
收藏 26KB RAR 举报
温馨提示
【语言】: vc 【功能】: 打印 【介绍】: 使用HDC手动画的打印功能
资源推荐
资源详情
资源评论
收起资源包目录
PreviewDemo.rar (21个子文件)
codefans.net
PreviewDemo
StdAfx.cpp 251B
PreParent.h 2KB
PreGoto.h 1KB
PreviewDemo.rc 7KB
PreviewDemoDlg.cpp 17KB
PreviewDemo.cpp 2KB
PreView.h 3KB
PreviewDemo.dsp 5KB
StdAfx.h 2KB
Resource.h 1KB
PreGoto.cpp 2KB
PreView.cpp 13KB
PreviewDemo.h 1KB
res
PreviewDemo.ico 1KB
PreviewDemo.rc2 403B
PreParent.cpp 10KB
PreviewDemoDlg.h 1KB
PreviewDemo.dsw 530B
IMAGE
preview.ico 1KB
bmp_back.bmp 150B
toolbar1.bmp 958B
共 21 条
- 1
资源评论
- sawyer08012013-08-08内容简单易懂,结构清晰可见。
- deity1102012-11-07不怎么好用!
一开始的回忆
- 粉丝: 19
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功