在一个基于对话框的程序中用一个按钮控制打印,打印内容从一个文本文件中读取。我使用了VC知识库提供的实例的打印类可以实现简单的打印功能,但是无法控制分页并且会打印出文本中的控制字符如制表符、回车符、换行符等,求解决方法!
谢谢!

解决方案 »

  1.   

    自己顶一下,顺便把代码贴出来
    void  CPrinterDlg::PrintFile()
    {  
        // TODO: Add your control notification handler code here
    CPrintRX m_Print;
    int x_pos[8];
    int y_pos[41];
    int i;
    //设置横坐标
    for (i=0; i< 30; i++)
    y_pos[i] = 60 + (20 * i);
             //设置纵坐标
    x_pos[0] = 45;
    x_pos[1] = x_pos[0] + 84;
    x_pos[2] = x_pos[1] + 68;
    x_pos[3] = x_pos[2] + 48;
    x_pos[4] = x_pos[3] + 53;
    x_pos[5] = x_pos[4] + 58;
    x_pos[6] = x_pos[5] + 84;
    x_pos[7] = x_pos[6] + 75;
    CRect StrRect;              //Draw text in Rect

    CStdioFile stdFile;
    CFileException ex;

    if(!stdFile.Open("D:\\input.txt", CFile::modeRead, &ex))
    {
    CString errormessage;
    ex.GetErrorMessage(errormessage.GetBuffer(1024), 1024);
    AfxMessageBox(errormessage);
    return;
    }
    CString str;

    if (m_Print.InitToPrint(NULL, 1) == -1)
    return;
    m_Print.StartPrint();
    for( int k = 0; k < 1; k++)
    {
    m_Print.StartPage();
    StrRect.SetRect(140, 20, 260, 50);
    m_Print.DrawText("hello!World!", StrRect, 0, 3, FORMAT_HCENTER|FORMAT_VCENTER);
    for(int j = 0; j < 3; j++)
    {
    stdFile.ReadString(str.GetBuffer(80),  80);
    StrRect.SetRect(x_pos[0], y_pos[j], x_pos[7], y_pos[j+1]);
    m_Print.DrawText((LPTSTR)(LPCTSTR)str, StrRect, 1, 0, FORMAT_NORMAL);
    }
    m_Print.EndPage();
    }
    m_Print.EndPrint();
    stdFile.Close();

    CPrintRX类的链接是http://www.vckbase.com/document/viewdoc/?id=1041
      

  2.   

    使用gdi+可以方便的实现打印功能,只要用打印DC初始化Graphics对象即可.下面是一个简单的例子:#include "gdiplus.h"
    DOCINFO docInfo; ZeroMemory(&docInfo, sizeof(DOCINFO));
    docInfo.cbSize = sizeof(DOCINFO);
    //打印文档名
    docInfo.lpszDocName = "GdiplusPrint"; //建立打印对话框
    PRINTDLG printDlg;
    ZeroMemory(&printDlg, sizeof(PRINTDLG));
    printDlg.lStructSize = sizeof(PRINTDLG);
    //需要返回DC
    printDlg.Flags = PD_RETURNDC; //显示打印对话框
    if(!PrintDlg(&printDlg))
    {
    AfxMessageBox("建立打印对话框失败");
    return;
    }
    else
    {
    //使用由打印对话框返回的设备环境句柄进行GDI+的所有操作
    //开始记录文档
    StartDoc(printDlg.hDC, &docInfo);
    StartPage(printDlg.hDC); //使用打印机设备环境句柄建立绘图平面类
    Graphics graphics(printDlg.hDC); //以下所有的输出都在打印机设备句柄中进行 //加载图片 Image image(L"1.jpg");
    //绘制图片
    graphics.DrawImage(&image, 0.0f, 0.0f); Pen pen(Color(255, 0, 0, 0));
    graphics.DrawRectangle(&pen, 200, 500, 200, 150); //开始打印
    EndPage(printDlg.hDC);
    EndDoc(printDlg.hDC);
    }
      

  3.   

    备注,int k = 0; k < 1; k++用来控制打印页数,这里我为了测试只打印一页,上限应该是打印页总数