无法使用 DWM 在自定义 window 框架上绘制

Can't draw on custom window frame with DWM

我使用 DWM 创建了自定义 window 框架。框架成功扩展,但每当我尝试在框架上绘制时,扩展框架会覆盖我尝试绘制的任何内容。我看到其他人试图在负边界内输入左上角,但即使我尝试这样做,标题栏仍然与主要 window 的绘画重叠。这是我的代码(注意:我没有任何用于命中测试的代码):

#include <Windows.h>
#include <numeric>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")

const auto s_brush = CreateSolidBrush(RGB(0, 0, 255));

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    LRESULT res;
    if (DwmDefWindowProc(hwnd, msg, wparam, lparam, &res))
        return res;

    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
    {
        RECT r;
        GetWindowRect(hwnd, &r);
        SetWindowPos(hwnd, 0, 0, 0, r.right - r.left, r.bottom - r.top, SWP_FRAMECHANGED);
    }
    break;
    case WM_ACTIVATE:
    {
        int metrics[4];
        const auto window_dpi_ = GetDpiForWindow(hwnd);

        metrics[0] = GetSystemMetricsForDpi(SM_CYCAPTION, window_dpi_);
        metrics[1] = GetSystemMetricsForDpi(SM_CXFIXEDFRAME, window_dpi_);
        metrics[2] = GetSystemMetricsForDpi(SM_CYSIZEFRAME, window_dpi_);
        metrics[3] = GetSystemMetricsForDpi(SM_CYBORDER, window_dpi_);

        const auto cy_titlebar_ = std::accumulate(metrics, metrics + sizeof metrics / sizeof(int), 0);
        MARGINS margins{ 0, 0, cy_titlebar_, 0 };
        DwmExtendFrameIntoClientArea(hwnd, &margins);
    }
    break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        const auto hdc = BeginPaint(hwnd, &ps);
        const auto old = SelectObject(hdc, s_brush);
        Rectangle(hdc, 0, 0, 50, 75);
        SelectObject(hdc, old);
        EndPaint(hwnd, &ps);
    }
    break;
    case WM_NCCALCSIZE:
        if (wparam == TRUE)
        {
            RECT& client_rect = reinterpret_cast<LPNCCALCSIZE_PARAMS>(lparam)->rgrc[0];
            const auto window_dpi_ = GetDpiForWindow(hwnd);
            const auto frame_width{ GetSystemMetricsForDpi(SM_CXFRAME, window_dpi_) };
            const auto border_width{ GetSystemMetricsForDpi(SM_CXPADDEDBORDER, window_dpi_) };
            const auto frame_height{ GetSystemMetricsForDpi(SM_CYFRAME, window_dpi_) };

            client_rect.bottom -= frame_height + border_width;
            client_rect.left += frame_width + border_width;
            client_rect.right -= frame_width + border_width;

            break;
        }
    default:
        return DefWindowProcW(hwnd, msg, wparam, lparam);
    }

    return 0;
}

int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE, LPWSTR lpcmdline, int cmd_show)
{
    WNDCLASS wc{ CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance,
    0,0, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), 0, L"CustWnd" };

    const auto hwnd = CreateWindow(MAKEINTATOM(RegisterClass(&wc)), L"Custom Window Frame", WS_OVERLAPPEDWINDOW,
        0, 0, 500, 700, 0, 0, hinstance, 0);

    ShowWindow(hwnd, cmd_show);
    UpdateWindow(hwnd);

    MSG msg;

    while (GetMessageW(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    return 0;
}

window class 没有默认光标,它会在您移动鼠标时显示错误的光标。将 wc 更改为

WNDCLASS wc{ CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance, 0, 
    LoadCursor(NULL, IDC_ARROW), 
    reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), 0, L"CustWnd" };

WM_NCHITTEST也要处理,否则title-bar抓不住。最好根据 Windows 样式计算边框粗细,或将其保留为 static 值,因为整个过程都需要它,以及标题栏高度。

请注意,此代码在 Windows 10 与 Window 7 中看起来非常不同,后者具有奇怪的透明 title-bar,您需要具有 alpha 通道的 32 位位图借鉴 title-bar。或者使用带 BufferedPaintSetAlpha 的缓冲涂料,如下所示

#include <Windows.h> 
#include <Windowsx.h> //for `GET_X_LPARAM` etc.
...
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static int cy_titlebar_ = 100;
    static RECT border_thickness;

    LRESULT result;
    if(DwmDefWindowProc(hWnd, msg, wParam, lParam, &result))
        return result;

    switch(msg)
    {
    case WM_CREATE:
    {
        //find border thickness
        border_thickness = { 0 };
        if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME)
        {
            AdjustWindowRectEx(&border_thickness,
                GetWindowLongPtr(hWnd, GWL_STYLE) & ~WS_CAPTION, FALSE, NULL);
            border_thickness.left *= -1;
            border_thickness.top *= -1;
        }
        else if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_BORDER)
        {
            border_thickness = { 1,1,1,1 };
        }

        MARGINS margins = { 0, 0, cy_titlebar_, 0 };
        DwmExtendFrameIntoClientArea(hWnd, &margins);
        SetWindowPos(hWnd, NULL, 0, 0, 0, 0, 
                    SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
        return 0;
    }

    case WM_NCCALCSIZE:
    {
        if(wParam)
        {
            RECT& r = reinterpret_cast<LPNCCALCSIZE_PARAMS>(lParam)->rgrc[0];
            r.left += border_thickness.left;
            r.right -= border_thickness.right;
            r.bottom -= border_thickness.bottom;
            return 0;
        }
        break;
    }

    case WM_NCHITTEST:
    {
        result = DefWindowProc(hWnd, msg, wParam, lParam);
        if(result == HTCLIENT)
        {
            POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            ScreenToClient(hWnd, &pt);
            if(pt.y < border_thickness.top) return HTTOP;
            if(pt.y < cy_titlebar_)  return HTCAPTION;
        }
        return result;
    }

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        auto hdc = BeginPaint(hWnd, &ps);

        //paint opaque:
        RECT rc{ 0, 0, 100, cy_titlebar_ };
        BP_PAINTPARAMS params = { sizeof(params), BPPF_NOCLIP | BPPF_ERASE };
        HDC memdc;
        HPAINTBUFFER hbuffer = BeginBufferedPaint(
                    hdc, &rc, BPBF_TOPDOWNDIB, &params, &memdc);

        auto brush = CreateSolidBrush(RGB(255, 0, 0));
        FillRect(memdc, &rc, brush);
        DeleteObject(brush);

        SetBkMode(memdc, TRANSPARENT);
        DrawText(memdc, L"Opaque", -1, &rc, 0);
        BufferedPaintSetAlpha(hbuffer, &rc, 255);
        EndBufferedPaint(hbuffer, TRUE);

        EndPaint(hWnd, &ps);
        return 0;
    }

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

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