Search code examples
gdidirectwrite

IDWriteGdiInterop::CreateBitmapRenderTarget failing


I have an application which renders glyphs to a printer device context. Earlier, it used GDI and it worked fine. But now, I'm trying to use Directwrite instead.

I've changed the existing ExtTextOut call and used IDWriteTextLayout::Draw function.

I need to pass an object implementing IDWriteTextRenderer interface in the draw function.

I've implemented the DrawGlyphRun callback in IDWriteTextRenderer interface using a call to IDWriteBitmapRenderTarget:: DrawGlyphRun. So, I need to create a IDWriteBitmapRenderTarget object.

I have the following code :

IDWriteFactory* pDWriteFactory = NULL;
IDWriteGdiInterop* pGdiInterop = NULL;
IDWriteTextLayout* pTextLayout = NULL;
IDWriteBitmapRenderTarget* pBitmapRenderTarget = NULL;
IDWriteRenderingParams* pRenderingParams = NULL;

hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
    __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&pDWriteFactory));

if (!SUCCEEDED(hr))
    pDWriteFactory = 0;
else
{
    hr = pDWriteFactory->GetGdiInterop(&pGdiInterop);
    if (!SUCCEEDED(hr))
        pGdiInterop = 0;
}

hr = pGdiInterop->CreateBitmapRenderTarget((HDC)hDC, someWidth, someHeight, &pBitmapRenderTarget);

The device context hDC is being passed by a function in another dll.

Result: hr=E_FAIL

Maybe I'm missing out on something very basic, but I have little knowledge of device contexts and I'm new to DirectWrite. I need to know why the same HDC structure which is compatible with a ExtTextOut call is not being compatible with DirectWrite.

Also, since the HDC field is optional, if I pass NULL, it succeeds but nothing gets rendered by the draw function. What does passing NULL signify ?


Solution

  • First make sure hDC is a valid memory device context, memory part is important. NULL argument means that target creates its own context compatible with screen/desktop. In any case nothing will be rendered automatically, you need to blit yourself from HDC returned by GetMemoryDC to your context, in other words target draws to this memory DC only, and what you do next is up to you.