分享

获取打印机设备环境DC的方法收集

 春夜喜雨LBR 2017-08-21

第一种

CPrintDialog m_PrintDlg(FALSE);

    CDC *pPrintDC;

    if (AfxGetApp()->GetPrinterDeviceDefaults(&m_PrintDlg.m_pd) == 0)

    {

        AfxMessageBox(L"没有安装打印机驱动程序或安装的打印机驱动程序出现问题.");

        pPrintDC = NULL;

        return;

    }

    pPrintDC = new CDC;

    pPrintDC->Attach(m_PrintDlg.CreatePrinterDC());

    if (pPrintDC){

        MessageBox(L"成功。");

   }第二种

CString str;

    CString strPrinter[3];

    LPWSTR szprinter = (LPWSTR)(LPCTSTR)str;

    // 定义一个设备环境句柄

    HDC hdcprint;

    //定义一个打印作业

    static DOCINFO di = { sizeof(DOCINFO), L"printer", NULL };

    // 得到设备字符串存入数组szprinter

    GetProfileStringW(L"windows", L"device", L",,,",szprinter, 80);

   

    //拆分字符串

    CString output = L"";

    for (int i = 0; i<3; i++)

    {

        AfxExtractSubString(output, str, i, ',');

        strPrinter[i]=output;

    }

    MessageBox(str);

    MessageBox(strPrinter[0]);

    MessageBox(strPrinter[1]);

    MessageBox(strPrinter[2]);

    //创建DC

    if ((hdcprint = CreateDC(strPrinter[1], strPrinter[0], strPrinter[2], NULL)) != 0)

    {

        MessageBox(L"设备环境获取成功。");

    }

第三种

CPrintDialog print(false);

  if (print.DoModal() == IDOK) {

      CDC printed;

      printed.Attach(print.GetPrinterDC());

      if (printed){

          MessageBox(L"成功");

      }

      printed.Detach();

  }

 第四种

//选择打印机对话框  

    CDC Dc;

    HDC hdcPrint = NULL;

    CPrintDialog dlg(FALSE);

    if (0)  //打印按钮,不弹出选择对话框,获取默认打印设备  

    {

        PRINTDLG printInfo;

        ZeroMemory(&printInfo, sizeof(printInfo));  //清空该结构  

        printInfo.lStructSize = sizeof(printInfo);

        printInfo.hwndOwner = 0;

        printInfo.hDevMode = 0;

        printInfo.hDevNames = 0;

        //这个是关键,PD_RETURNDC 如果不设这个标志,就拿不到hDC  

        //PD_RETURNDEFAULT 这个就是得到默认打印机,不需要弹出设置对话框  

        printInfo.Flags = PD_RETURNDC | PD_RETURNDEFAULT | PD_ALLPAGES;

 

        PrintDlg(&printInfo);//调用API拿出默认打印机  

        DWORD rst = CommDlgExtendedError();//看看出错没有  

        if (rst != 0)

        {//出错了,清空标志再次调用API,此时就会弹出打印设置对话框供用户选择了  

            printInfo.Flags = 0;

            PrintDlg(&printInfo);

        }

        hdcPrint = printInfo.hDC; //得到打印DC,输出到打印  

    }

    else  //弹出对话框选择打印设备  

    {

        dlg.DoModal();

        hdcPrint = dlg.GetPrinterDC();

    }

    if (hdcPrint == NULL)

    {

        MessageBox(_T("打印机初始化失败!"));

        return;

    }

    Dc.Attach(hdcPrint);

    if (Dc){

        MessageBox(L"ok");

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多