【问题标题】:MFC - Printing a CBitmap to printer CDCMFC - 将 CBitmap 打印到打印机 CDC
【发布时间】:2016-03-11 07:29:43
【问题描述】:

我正在使用以下代码打印剪贴板(位图)内的内容。
在打印的纸上,我看到用 MoveTo/LineTo 绘制的黑线,但未绘制位图。
在 CView 中使用相同的绘图代码效果很好。

keybd_event(VK_SNAPSHOT, 1, 0, NULL);
HBITMAP handle = NULL;

if (::OpenClipboard(NULL))
{
    handle = (HBITMAP)GetClipboardData(CF_BITMAP);

    if (handle)
    {
        CBitmap* pBmp = CBitmap::FromHandle(handle);
        BITMAP bm;
        pBmp->GetBitmap(&bm);
        int iBmpWidth = bm.bmWidth;
        int iBmpHeight = bm.bmHeight;

        CPrintDialog* pDlg = new CPrintDialog(FALSE);
        CString csText;
        CString cTitle;

        if (pDlg->GetDefaults() == FALSE)
        {
            delete pDlg;
            return;
        }

        pDlg->m_pd.Flags &= ~PD_RETURNDEFAULT;
        LPDEVMODE pDevMode = pDlg->GetDevMode();
        ::GlobalUnlock(pDlg->m_pd.hDevMode);

        DOCINFO di;
        di.cbSize = sizeof(DOCINFO);
        pDlg->m_pd.hwndOwner = this->GetSafeHwnd();

        if (pDlg->DoModal() == IDOK)
        {
            HDC hdcPrinter = pDlg->GetPrinterDC();

            if (hdcPrinter != NULL)
            {
                pDevMode = (LPDEVMODE)GlobalLock(pDlg->m_pd.hDevMode);
                pDevMode->dmPaperSize = DMPAPER_A4;             
                pDevMode->dmOrientation = DMORIENT_LANDSCAPE;   
                ResetDCW(hdcPrinter, pDevMode);                 
                GlobalUnlock(pDlg->m_pd.hDevMode);

                // create a CDC and attach it to the default printer
                CDC dcPrinter;
                dcPrinter.Attach(hdcPrinter);

                // call StartDoc() to begin printing
                DOCINFO docinfo;
                memset(&docinfo, 0, sizeof(docinfo));
                docinfo.cbSize = sizeof(docinfo);
                docinfo.lpszDocName = _T("CDC::StartDoc() Code Fragment");

                // if it fails, complain and exit gracefully
                if (dcPrinter.StartDoc(&docinfo) < 0)
                {
                    MessageBox(_T("Printer wouldn't initalize"));
                }
                else
                {
                    // start a page
                    if (dcPrinter.StartPage() < 0)
                    {
                        MessageBox(_T("Could not start page"));
                        dcPrinter.AbortDoc();
                    }
                    else
                    {

                        int PaperWidth = dcPrinter.GetDeviceCaps(HORZRES);
                        int PaperHeight = dcPrinter.GetDeviceCaps(VERTRES);             

                        CDC memDC;
                        memDC.CreateCompatibleDC(&dcPrinter);
                        CBitmap* pOldBit = memDC.SelectObject(pBmp);

                        dcPrinter.MoveTo(1000, 1000);
                        dcPrinter.LineTo(PaperWidth - 1000, PaperHeight - 1000);

                        dcPrinter.StretchBlt(100, 
                                                100, 
                                                PaperWidth - 100, 
                                                PaperHeight - 100, 
                                                &memDC, 
                                                0, 
                                                0, 
                                                iBmpWidth, 
                                                iBmpHeight, 
                                                SRCCOPY);

                        memDC.SelectObject(pOldBit);
                        memDC.DeleteDC();

                        dcPrinter.EndPage();
                        dcPrinter.EndDoc();
                    }
                }
            }
        }

        delete pDlg;
    }

    ::EmptyClipboard();
    ::CloseClipboard();
}

【问题讨论】:

    标签: printing mfc


    【解决方案1】:

    使用您的代码而不做任何更改对我来说很有效,我使用真正的打印机和 CutePDF 进行了测试,两者都打印了位图。创建 memDC 时,源 DC 可能存在问题,它不支持正确的色彩空间或不支持光栅操作。请尝试以下代码:

    CDC* pDC = GetDesktopWindow()->GetDC();
    memDC.CreateCompatibleDC(pDC);
    GetDesktopWindow()->ReleaseDC(pDC);
    

    【讨论】:

    • Richard 的代码出现了相同的行为。
      我将在另一台计算机上尝试。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多