Southwind1984
Southwind1984

Reputation: 71

How to use direct 2d to draw text in the printer DC?

I use the direct 2d API to draw text in the GDI dc correctly,

but When I use the same code to draw text in the printer DC, it failed,

The printer hdc is create as below,

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

The Direct 2d draw text code snippet is as below

    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();
    }

Unfortunately, the p_dcTarget can not bind the hdc correctly, so there is nothing printed out,

but if the hdc is the GDI dc to draw text in the window, it will bind successfully and then draw the correct text out.

Is there anything different when binding the printer DC?

Any suggestion for this trouble? Thanks.

Upvotes: 4

Views: 755

Answers (1)

Josh Kelley
Josh Kelley

Reputation: 58382

According to this discussion on MSDN, you can't print directly from Direct2D to a printer DC. Instead, you'll need to render to an in-memory bitmap then copy that bitmap to the printer with BitBlt or StretchBlt.

The MSDN documentation on GDI and Direct2D interoperability points out that, even if Direct2D did work with printer DCs, it would be doing this internally:

When you use an ID2D1DCRenderTarget, it renders Direct2D content to an internal bitmap, and then renders the bitmap to the DC with GDI.

Upvotes: 4

Related Questions