Using Bitmap Brushes
Direct2D 中的图片处理增加了很多的灵活的特性,现在的Direct2D可以很好的和GUI,GUI+以及Direct3D混合使用,它支持更多的图形格式,更丰富的绘制方法。使用Direct2D绘制的win7程序,显示效果更好,绘制速度更快。
LRESULT InItD2D(HWND hWnd) { //创建一个Direct2D资源指针 HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory); CoInitialize(NULL); hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&p_pImageFactory) ); if(SUCCEEDED(hr)) { // Create a DirectWrite factory. hr = DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED, __uuidof(p_pDWriteFactory), reinterpret_cast
其中IWICImagingFactory类提供了一个高效的图形绘制接口
hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&p_pImageFactory) );
还有两个函数LoadResourceBitmap()和LoadBitmapFromFile()都是载入图片的函数,这两个函数可以载入我们常见的图片格式,要研究这一块的内容,估计一时半会说不明白,微软现在已经通过类似于控件的方式提供了多种图片的载入方式,目前直接支持的有jpg,bmp,gif,png等。还可以按照自己的解析器来完成自己需要图片格式的读写~
VOID Rander() { if(!p_pRenderTarget) return ; static const WCHAR szBitmapBrushText[] = L"图片笔刷"; // Define the shape of rectangles to be filled with brushes. D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 470, 613); // Define the area where captions are drawn. D2D1_RECT_F rcTextRect = D2D1::RectF(0, 1065, 450, 200); p_pRenderTarget->BeginDraw(); p_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); p_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); // Translate for the bitmap brush. p_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(150, 50))); // Draw a caption. //Demonstrate a bitmap brush. p_pRenderTarget->FillRectangle(&rcBrushRect, p_pBitmapBrush); p_pRenderTarget->DrawRectangle(&rcBrushRect, p_pBlackBrush, 1, NULL); p_pRenderTarget->DrawText( szBitmapBrushText, ARRAYSIZE(szBitmapBrushText) - 1, p_pText, &rcTextRect, p_pBlackBrush ); p_pRenderTarget->EndDraw(); }