【问题标题】:Direct2D: moving window turns grayDirect2D:移动窗口变灰
【发布时间】:2017-05-22 21:07:06
【问题描述】:

我从一个非常简单的例子开始了 Direct2D。 获取工厂和 ID2D1HwndRenderTarget,然后处理 WM_PAINT 消息,使用“清除”功能仅绘制纯色背景。

一切正常,直到我开始移动窗口。当窗口移动时,它会变成灰色,就像什么都没有画一样。我试着画了一个椭圆,结果是一样的。

如何在窗口移动时呈现窗口内容?

附:万一需要代码

#include <Windows.h>

#include <d2d1_1.h> 
#pragma comment(lib,"d2d1")

ID2D1Factory * d2factory_ptr = NULL;
ID2D1HwndRenderTarget * renderTarget_ptr = NULL;


LRESULT CALLBACK mainWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(
    HINSTANCE hInstance
    , HINSTANCE prevInstance
    , LPWSTR cmd
    , int nCmdShow
) {
    WNDCLASSEX wndClassStruct;
    ZeroMemory(&wndClassStruct, sizeof(WNDCLASSEX));
    wndClassStruct.cbSize = sizeof(WNDCLASSEX);
    wndClassStruct.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wndClassStruct.style = CS_HREDRAW | CS_VREDRAW;
    wndClassStruct.hInstance = hInstance;
    wndClassStruct.lpfnWndProc = mainWinProc;
    wndClassStruct.lpszClassName = TEXT("MainWnd");

    RegisterClassEx(&wndClassStruct);

    RECT windowRect = { 0,0,640,480};
    AdjustWindowRectEx(&windowRect, WS_OVERLAPPEDWINDOW, 0, WS_EX_APPWINDOW);
    HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, TEXT("MainWnd"), TEXT("Direct 2D Test Window"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, CW_USEDEFAULT, 0, windowRect.right-windowRect.left, windowRect.bottom-windowRect.top, NULL, NULL, hInstance, 0);

    {
        D2D1_FACTORY_OPTIONS fo;
        ZeroMemory(&fo, sizeof(D2D1_FACTORY_OPTIONS));

        IID const factoryIID = IID_ID2D1Factory1;

        HRESULT res = S_OK;
        if (S_OK != (res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factoryIID, &fo, &d2factory_ptr))) {
            return 0;
        }

        RECT clientRect;
        GetClientRect(hWnd, &clientRect);

        D2D1_RENDER_TARGET_PROPERTIES renderTargetProps;
        ZeroMemory(&renderTargetProps, sizeof(D2D1_RENDER_TARGET_PROPERTIES));
        renderTargetProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
        renderTargetProps.pixelFormat = (D2D1_PIXEL_FORMAT) { DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED };
        renderTargetProps.dpiX = 0;
        renderTargetProps.dpiY = 0;
        renderTargetProps.usage = D2D1_RENDER_TARGET_USAGE_FORCE_BITMAP_REMOTING;
        renderTargetProps.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;

        D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderProps;
        ZeroMemory(&hwndRenderProps, sizeof(D2D1_HWND_RENDER_TARGET_PROPERTIES));
        hwndRenderProps.hwnd = hWnd;
        hwndRenderProps.pixelSize = (D2D1_SIZE_U) { clientRect.right - clientRect.left, clientRect.bottom - clientRect.top };
        hwndRenderProps.presentOptions = D2D1_PRESENT_OPTIONS_NONE;

        if (S_OK != (res = d2factory_ptr->lpVtbl->CreateHwndRenderTarget(d2factory_ptr, &renderTargetProps, &hwndRenderProps, &renderTarget_ptr))) {
            return 0;
        }
    }

    ShowWindow(hWnd, nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    DestroyWindow(hWnd);

    if(NULL != renderTarget_ptr) 
        renderTarget_ptr->lpVtbl->Base.Base.Base.Release((IUnknown*)renderTarget_ptr);

    if (NULL != d2factory_ptr)
        d2factory_ptr->lpVtbl->Base.Release((IUnknown*)d2factory_ptr);

    return 0;
}

LRESULT onPaintMainWindow() {
    ID2D1RenderTargetVtbl renderTargetFuncs = renderTarget_ptr->lpVtbl->Base;
    ID2D1RenderTarget * This = (ID2D1RenderTarget*)renderTarget_ptr;
    D2D1_TAG tag1, tag2;

    D2D1_COLOR_F backgroundClr = (D2D1_COLOR_F) { 0.0, 0.5, 1.0, 1.0 };
    renderTargetFuncs.BeginDraw(This);
    renderTargetFuncs.Clear(This, &backgroundClr);
    renderTargetFuncs.EndDraw(This, &tag1, &tag2);

    return 0;
}

LRESULT CALLBACK mainWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    if (WM_PAINT == uMsg)
        return onPaintMainWindow();

    if (WM_DESTROY == uMsg) {
        PostQuitMessage(0); return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

【问题讨论】:

  • 我们需要查看代码。

标签: winapi direct2d


【解决方案1】:

将您的 WNDCLASSEX 配置为没有背景画笔。

替换这一行:

wndClassStruct.hbrBackground = (HBRUSH)COLOR_WINDOW;

有了这个:

wndClassStruct.hbrBackground = GetStockObject(NULL_BRUSH);

或者,您可以修改 mainWndProc 以吞下 WM_ERASEBKGND 消息。它通过在发出 WM_PAINT 之前不允许窗口自行擦除来达到相同的效果。

LRESULT CALLBACK mainWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    if (WM_PAINT == uMsg)
        return onPaintMainWindow();

    if (uMsg == WM_ERASEBKGND)
    {
        // ignore requests to erase the background since the wm_paint
        // handler is going to redraw the entire window.
        return 0;
    }

    if (WM_DESTROY == uMsg) {
        PostQuitMessage(0); return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

【讨论】:

  • 谢谢!两者都在工作。什么是更好的决定? NULL_BRUSH 还是 WM_ERASEBKGND?
  • 我会使用 WM_ERASEBKGND 修复,因为这意味着 Windows 甚至不会尝试进行像素更新。您应该阅读this,因为它建议您应该返回 1 而不是 0。
  • 非常感谢!我将处理 WM_ERASEBKGND 消息。
猜你喜欢
  • 2010-11-09
  • 1970-01-01
  • 2012-11-14
  • 2014-02-09
  • 2013-01-19
  • 1970-01-01
  • 2014-01-23
  • 2013-02-21
  • 1970-01-01
相关资源
最近更新 更多