从c方式创建窗体,创建默认消息处理:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_ERASEBKGND:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

 

创建d2d资源:

CoInitialize(NULL);
ID2D1Factory* pD2DFactory = NULL;
ID2D1DCRenderTarget* pRenderTarget = NULL;
ID2D1SolidColorBrush* pWhiteBrush = NULL;
D2D1_SIZE_U size = SizeU(_nScrWidth, _nScrHeight);

HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
if (hr < 0) { goto EXIT_ENTRY; }

hr = pD2DFactory->CreateDCRenderTarget(
&RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)),
&pRenderTarget);

if (hr < 0) { goto EXIT_ENTRY; }

hr = pRenderTarget->CreateSolidColorBrush(ColorF(0xffffffff, 1.0), &pWhiteBrush);

if (hr < 0) { goto EXIT_ENTRY; }

 

整个程序只创建一次内存dc,然后每次更新都清空这个DC就行,不需要反复创建删除,这样可以高效地使用d2d,不再需要gdi绘制函数了

    while (GetAsyncKeyState(VK_ESCAPE) >= 0)
    {
        pRenderTarget->BeginDraw();
        pRenderTarget->Clear(0);
        
        UpdateSnow_D2D(pRenderTarget, pWhiteBrush, snowSmall, SnowSize1Pixel);
        UpdateSnow_D2D(pRenderTarget, pWhiteBrush, snowBig, SnowSize4Pixel);

        if (pRenderTarget->EndDraw() < 0) {
            break;
        }

        UpdateLayeredWindowIndirect(hwnd, &layeredInfo);

        Sleep(30);
    }

 

台式机测试:奔腾双核下,纯GDI渲染CPU占用率30%-50%,平均35%。使用D2D渲染占用率10%-30%,平均15%。

 

posted on 2012-01-13 13:06  asight  阅读(1258)  评论(0编辑  收藏  举报