最新消息:想得多,做的少。一天到晚瞎鸡巴搞。

MFC对话框使用CPrintDialog实现打印,指定打印机、后台打印

Windows编程 阿虚 878浏览 0评论

对话框打印,网上一搜一大堆,基本分2类:

A类: CPrintDialog.DoModal,然后在模态对话框里选打印机、打印配置;

B类:GetPrinterDeviceDefaults,调用默认打印机。

我的工作内容是理解以上2类后,再根据MSDN,实现MDF对话框后台指定打印机打印。

废话不多说,上菜~

功能:基于对话框的MFC打印(非文档视图结构),指定打印机,后台打印(不弹出对话框)

思路:

1、枚举打印机,并选择其中一个;

2、CPrintDialog实例指定到选中的打印机;

3、CPrintDialog后台打印

具体实现:

1、变量(控件)。在对话框上添加一个combobox(IDC_COMBO1,对应变量m_cboPrint)、一个edit(IDC_EDIT1),edit允许回车,多行(代码就不贴了,知道MFC应该就懂);

2、在OnInitDialog里枚举打印机设备,如果报函数未定义,加入头文件#include <winspool.h>

需要调用两次EnumPrinters函数,第一次的到结构体的大小,第二次得到打印机列表

BOOL CSuperMarkets::OnInitDialog()
{
// TODO: 在此添加额外的初始化代码  
    DWORD dwNeeded;  
    DWORD dwReturn;  
    DWORD dwFlag = PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL;  
  
    EnumPrinters(dwFlag, NULL, 4, NULL, 0, &dwNeeded, &dwReturn);  
  
    PRINTER_INFO_4* p4;  
    p4 = new PRINTER_INFO_4[dwNeeded];  
    EnumPrinters(dwFlag, NULL, 4, (PBYTE)p4, dwNeeded, &dwNeeded, &dwReturn);  
  
    for (int i = 0; i<(int)dwReturn; i++) this->m_cboPrint.AddString(p4[i].pPrinterName);  
  
    delete []p4;  

    //下面是获取默认打印机
    //构造打印设置对话框对象
    CPrintDialog printDialog(FALSE);                      
    //获取默认打印设备的设备环境
    printDialog.GetDefaults();                              
    //获取当前打印机的名称
    m_DYJ = printDialog.GetDeviceName();    
}

3、操作对话框,在IDC_EDIT1里输入打印的内容,在IDC_COMBO1里选中打印机;

4、打印(我是用OK按钮打印的,大家随便)

//打印账单
void CSuperMarkets::OnBnClickedButton6()
{
    CString strMessage;  

    //m_DYJStr  被打印的字符串
    strMessage = m_DYJStr;
    strMessage += "\r\n";       //添加结尾,方便后面循环读取打印数据  

    //this->GetDlgItem(IDC_COMBO1)->GetWindowText(strPrintDevice);  

    //打印配置界面的按钮可用性,因为后台打印,其实这个配置没什么意义  
    DWORD dwFlag = PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_HIDEPRINTTOFILE;      

    //CPrintDialog实例化,因为MFC的打印设备无关性,可以理解为这就是一台打印机 
    CPrintDialog pPrintdlg(FALSE, dwFlag, this);                                             

    HGLOBAL hDevMode = NULL;  
    HGLOBAL hDevNames = NULL;  

    //获得指定打印机的配置、名字 
    if (GetPrinterDevice(m_DYJ.GetBuffer(0), &hDevNames, &hDevMode))                
        AfxGetApp()->SelectPrinter(hDevNames, hDevMode);  
    else  
        AfxMessageBox(_T("Failed to select custom printer"));  

    m_DYJ.ReleaseBuffer();  

    //让pPrintdlg使用我们指定的打印机
    pPrintdlg.m_pd.hDevMode = hDevMode;                                                       
    pPrintdlg.m_pd.hDevNames = hDevNames;  

    CDC dc;  
    //后台打印创建法,如果需要弹出打印对话框,请用DoModal
    dc.Attach(pPrintdlg.CreatePrinterDC());                                                   

    //下面的内容网上很多,就不解释了
    DOCINFO di;                                                                               
    di.cbSize = sizeof(DOCINFO);  
    di.lpszDocName = _T("有驱打印测试");  
    di.lpszDatatype = NULL;  
    di.lpszOutput = NULL;  
    di.fwType = 0;  

    dc.StartDoc(&di);  
    dc.StartPage();  
    dc.SetMapMode(MM_TEXT);  

    CRect recPrint(0, 0, dc.GetDeviceCaps(LOGPIXELSX), dc.GetDeviceCaps(LOGPIXELSY));  
    dc.DPtoLP(&recPrint);  
    dc.SetWindowOrg(0, 0);  

    CFont newFont;  
    //设置字体和字体大小
    VERIFY(newFont.CreatePointFont(50, _T("宋体"), &dc));  
    CFont* oldFont = dc.SelectObject(&newFont);  

    dc.SetTextAlign(TA_TOP | TA_LEFT);  

    CString strPrint;  
    int nIndex = 0;  
    int x = 20;  //设置侧边间距
    int y = 50;  
    CSize textSize;  
    //根据当前字体的宽、高,后面以此高度为行高
    textSize = dc.GetTextExtent(_T("00"), 2);                             

    //将IDC_EDIT1编辑框中内容打印,支持换行,一次换行等于'\r\n',所以在开头strMessage += _T("\r\n")
    while ((nIndex = strMessage.Find(_T("\r\n"))) > -1)                    
    {  
        strPrint = strMessage.Left(nIndex);  
        strMessage = strMessage.Mid(nIndex+2);  

        dc.TextOut(x, y, strPrint);  

        //下移一行,行高为字体高度
        y += textSize.cy;                                                 
    }  

    dc.SelectObject(oldFont);  
    newFont.DeleteObject();  
    dc.EndPage();  
    dc.EndDoc();  
    DeleteDC(dc.Detach());  
}


BOOL CSuperMarkets::GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)  
{  
    // if NULL is passed, then assume we are setting app object's  
    // devmode and devnames  
    if (phDevMode == NULL || phDevNames == NULL)  
        return FALSE;  

    // Open printer  
    HANDLE hPrinter;  
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)  
        return FALSE;  

    // obtain PRINTER_INFO_2 structure and close printer  
    DWORD dwBytesReturned, dwBytesNeeded;  
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);  
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,  
        dwBytesNeeded);  
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,  
        &dwBytesReturned) == 0) {  
            GlobalFree(p2);  
            ClosePrinter(hPrinter);  
            return FALSE;  
    }  
    ClosePrinter(hPrinter);  

    // Allocate a global handle for DEVMODE  
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) +  
        p2->pDevMode->dmDriverExtra);  
    ASSERT(hDevMode);  
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);  
    ASSERT(pDevMode);  

    // copy DEVMODE data from PRINTER_INFO_2::pDevMode  
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) +  
        p2->pDevMode->dmDriverExtra);  
    GlobalUnlock(hDevMode);  

    // Compute size of DEVNAMES structure from PRINTER_INFO_2's data  
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name  
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name  
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name  

    // Allocate a global handle big enough to hold DEVNAMES.  
    HGLOBAL hDevNames = GlobalAlloc(GHND,  
        sizeof(DEVNAMES) +  
        (drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));  
    ASSERT(hDevNames);  
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);  
    ASSERT(pDevNames);  

    // Copy the DEVNAMES information from PRINTER_INFO_2  
    // tcOffset = TCHAR Offset into structure  
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);  
    ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR));  

    pDevNames->wDriverOffset = tcOffset;  
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,  
        drvNameLen*sizeof(TCHAR));  
    tcOffset += drvNameLen;  

    pDevNames->wDeviceOffset = tcOffset;  
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,  
        ptrNameLen*sizeof(TCHAR));  
    tcOffset += ptrNameLen;  

    pDevNames->wOutputOffset = tcOffset;  
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,  
        porNameLen*sizeof(TCHAR));  
    pDevNames->wDefault = 0;  

    GlobalUnlock(hDevNames);  
    GlobalFree(p2);   // free PRINTER_INFO_2  

    // set the new hDevMode and hDevNames  
    *phDevMode = hDevMode;  
    *phDevNames = hDevNames;  
    return TRUE;  
}  

—–

感谢原作者提供的源码:http://blog.csdn.net/xjkstar/article/details/25828097

转载请注明:虚无 » MFC对话框使用CPrintDialog实现打印,指定打印机、后台打印

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址