【问题标题】:How to use direct 2d to draw text in the printer DC?如何使用直接 2d 在打印机 DC 中绘制文本?
【发布时间】:2017-06-22 14:30:53
【问题描述】:

我使用直接 2d API 在 GDI dc 中正确绘制文本,

但是当我使用相同的代码在打印机 DC 中绘制文本时,它失败了,

打印机 hdc 如下创建,

   hdc = CreateDC("WINSPOOL", printerName, "", pdevMode);

直接二维绘制文本代码sn-p如下

    if (p_d2dFactory == NULL) {
        hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &p_d2dFactory);
    }      

    if (SUCCEEDED(hr) && p_dcTarget == NULL) {
        hr = p_d2dFactory->CreateDCRenderTarget(&props, &p_dcTarget);
    }

    if (SUCCEEDED(hr)) {
        hr = p_dcTarget->BindDC(hdc, &rc);
    }

    if (SUCCEEDED(hr)) {      
        p_dcTarget->BeginDraw();
        p_dcTarget->Clear(NULL);
        p_dcTarget->DrawTextLayout(origin, g_pTextLayout,p_dbrush);
        hr= p_dcTarget->EndDraw();
    }

可惜p_dcTarget无法正确绑定hdc,所以什么也没有打印出来,

但如果hdc是在窗口中绘制文本的GDI dc,它会绑定成功,然后绘制正确的文本。

绑定打印机DC有什么不同吗?

对这个麻烦有什么建议吗?谢谢。

【问题讨论】:

    标签: printing direct2d gdi


    【解决方案1】:

    根据this discussion on MSDN,您不能直接从 Direct2D 打印到打印机 DC。相反,您需要渲染到内存位图,然后使用BitBltStretchBlt 将该位图复制到打印机。

    MSDN documentation on GDI and Direct2D interoperability 指出,即使 Direct2D 确实适用于打印机 DC,它也会在内部执行此操作:

    当您使用 ID2D1DCRenderTarget 时,它会将 Direct2D 内容呈现到内部位图,然后使用 GDI 将位图呈现到 DC。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多