c – Direct2D拒绝在窗口上绘制位图,无声地失败

我试图用Direct2D在一个窗口上绘制128×128位图.但是,没有任何显示,EndDraw()不会返回任何错误.

这是我的WM_PAINT代码:

case WM_PAINT:
        D2D1_RECT_F testd2dbuttonrect;

        mainwRT->BeginDraw();
        mainwRT->SetTransform( D2D1::Matrix3x2F::Identity() );

        testd2dbutton.pd2drectgm->GetRect( &testd2dbuttonrect );
        mainwRT->FillRectangle( &testd2dbuttonrect, pSolidBrush );

        //This is where I'm trying to draw my bitmap
        mainwRT->FillRectangle( D2D1::RectF(0.0f,0.0f,127.0f,127.0f), pBgndBrush );

        errmsg = mainwRT->EndDraw();
        if( !SUCCEEDED(errmsg) )
            printf("EndDraw() error: %d\r\n", errmsg );
        break;

我怀疑这可能是因为我从Visual Studio资源加载的位图数据是垃圾,但我无法知道,因为没有任何失败并提供错误消息.这是我用来从资源加载ID2D1Bitmap的代码:

int LoadBitmapFromResource( IWICImagingFactory *pImageFactory, ID2D1RenderTarget *pRT, int resID, ID2D1Bitmap **ppD2DBitmap )
{
    int errmsg;

    HBITMAP hbitmap;
    WICBitmapAlphaChannelOption wicalpha;
    IWICBitmap *pwicbitmap;
    IWICBitmapSource *pconvertedwicbitmap;
    IWICFormatConverter *pConverter;

    ID2D1Factory *d2dfactory;
    D2D1_BITMAP_PROPERTIES d2dbp;
    D2D1_PIXEL_FORMAT d2dpf;
    FLOAT dpiX;
    FLOAT dpiY;

    hbitmap = LoadBitmap( GetModuleHandle(NULL), MAKEINTRESOURCEW(resID) );
    wicalpha = WICBitmapUseAlpha;

    errmsg = pImageFactory->CreateBitmapFromHBITMAP( hbitmap, NULL, wicalpha, &pwicbitmap );
    if( !SUCCEEDED(errmsg) )
    {
        printf("LoadBitmapFromResource::CreateBitmapFromHBITMAP() error: %x\r\n", errmsg );
        return errmsg;
    }

    errmsg = pImageFactory->CreateFormatConverter( &pConverter );
    if( !SUCCEEDED(errmsg) )
    {
        printf("LoadBitmapFromResource::CreateFormatConverter() error: %x\r\n", errmsg );
        return errmsg;
    }

    d2dpf.format = DXGI_FORMAT_B8G8R8A8_UNORM;
    d2dpf.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
    pRT->GetFactory( &d2dfactory );
    d2dfactory->GetDesktopDpi( &dpiX, &dpiY );
    d2dbp.pixelFormat = d2dpf;
    d2dbp.dpiX = dpiX;
    d2dbp.dpiY = dpiY;

    pConverter->Initialize( pwicbitmap, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeMedianCut );
    if( !SUCCEEDED(errmsg) )
    {
        printf("LoadBitmapFromResource::Initialize() error: %x\r\n", errmsg );
        return errmsg;
    }

    errmsg = WICConvertBitmapSource( GUID_WICPixelFormat32bppPBGRA, pwicbitmap, &pconvertedwicbitmap );
    if( !SUCCEEDED(errmsg) )
    {
        printf("LoadBitmapFromResource::WICConvertBitmapSource() error: %x\r\n", errmsg );
        return errmsg;
    }

    errmsg = pRT->CreateBitmapFromWicBitmap( pconvertedwicbitmap, &d2dbp, ppD2DBitmap );
    if( !SUCCEEDED(errmsg) )
    {
        printf("LoadBitmapFromResource::CreateBitmapFromWicBitmap() error: %x\r\n", errmsg );
        return errmsg;
    }

    pConverter->Release();
    pwicbitmap->Release();
    DeleteObject( hbitmap );

    return 0;
}

我在Visual Studio 2010中使用C与本机WinAPI.

最佳答案 我认为你仍然需要在WM_PAINT中使用经典的BeginPaint(),然后进行D2D渲染,然后调用EndPaint().这就是我的代码所做的,我认为它也是MSDN示例代码小发明所做的.

点赞