• Drawing in layered window with Direct2D


    从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%。

  • 相关阅读:
    springcloud的配置
    springboot面试题
    SqlServer取值四舍五入
    java导出Excel表格简单的方法
    pandas处理数据textrank提取关键词
    toarray()时出现memory error问题解决
    回溯法解决N皇后问题 C语言
    图论中四个最短路径算法
    第一个java程序
    js 实现简单屏蔽某个地区的访问
  • 原文地址:https://www.cnblogs.com/asight/p/2321477.html
Copyright © 2020-2023  润新知