我正在尝试使用 Direct2D 将 128x128 位图绘制到窗口上。但是,什么都没有显示,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。