VC++分页打印实现

  VC++分页打印实现:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
//打印对话框
DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_NOSELECTION | PD_HIDEPRINTTOFILE;
CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
    
//获得打印机DC
    HDC hDC = dlg.GetPrinterDC();
    
if (hDC == NULL)
    {
        
return;
    }
    
//通过HDC获得CDC指针
    CDC *pDC = CDC::FromHandle(hDC);
    pDC->m_bPrinting = TRUE;
    
//文档信息
    DOCINFO di;
    ::ZeroMemory (&di, 
sizeof (DOCINFO));
    di.cbSize = 
sizeof (DOCINFO);
    di.lpszDocName = _T(
"Demo");
    
//开始打印
    BOOL bPrinting = pDC->StartDoc(&di);
    
for (UINT nPage = 0; nPage < 10 && bPrinting; nPage++)
    {
        pDC->StartPage();
        
//输出文本
        CString strText = _T("");
        strText.Format(_T(
"第%d页"), nPage + 1);
        pDC->TextOut(
1000, strText);
        
for (int i = 0; i < 100; i++)
        {
            pDC->TextOut(
100100 * (i + 1), _T("Hello World!"));
        }
        bPrinting = (pDC->EndPage() > 
0);
    }

    
if (bPrinting)
    {
        pDC->EndDoc();
    }
    
else
    {
        pDC->AbortDoc();
    }
    
//结束打印
    pDC->EndDoc();
    
//删除DC
    pDC->DeleteDC();
}

posted on 2018-03-09 15:16  我来乔23  阅读(727)  评论(0编辑  收藏  举报

导航