5

WM_EX_TRANSPARENT在某些窗口上使用窗口样式以尝试进行具有透明度的双缓冲。但是,我遇到了一个问题,因为当我InvalidateRect在父窗口上执行时,子窗口不会重绘。

迭代子窗口并让它们重新绘制自己是父窗口的责任,还是我做错了?如果它是父窗口的责任,我怎样才能得到父无效矩形内的所有子窗口?

这是一个完全可编译的示例,说明了我正在谈论的行为。当您将鼠标移到父窗口上时,您可以看到该按钮消失了。如果单击它,该按钮会自行重绘,但当您将鼠标移开并返回父窗口时,它会再次消失。

#include <Windows.h>
    
LRESULT CALLBACK WndProc(HWND window, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        return 0;
    
    case WM_MOUSEMOVE:
        InvalidateRect(window, NULL, true);
    
        return 0;
    
    case WM_NCDESTROY:
        PostQuitMessage(0);
        break;
    }
    
    return DefWindowProc(window, uMsg, wParam, lParam);
}
    
int PASCAL WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int nShowCmd) {
    WNDCLASS wc;
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hinst;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "test_window";
    
    RegisterClass(&wc);
    
    HWND wnd = CreateWindowEx(WS_EX_TRANSPARENT, "test_window", "test", WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, 50, 50, 400, 400, NULL, NULL, hinst, NULL);
    
    ShowWindow(wnd, SW_SHOW);
    
    HWND btn = CreateWindowEx(WS_EX_TRANSPARENT, "BUTTON", "button 1", WS_CHILD, 50, 50, 100, 50, wnd, NULL, hinst, NULL);
    
    ShowWindow(btn, SW_SHOW);
    
    MSG m;
    
    while (GetMessage(&m, NULL, 0, 0)) {
        TranslateMessage(&m);
        DispatchMessage(&m);
    }
    
    return 0;
}

最终的解决方案

是按照下面建议的答案去做,然后在父窗口的WM_PAINT消息中向WM_PAINT每个孩子发送 a 并让孩子们在父母的双缓冲区中绘画(而不是在自己身上绘画任何东西),然后让父母BitBlt在它的缓冲区中(已被绘制到由它自己,然后由它的每个孩子从 Z 顺序的底部到顶部)进入它的HDC. 这允许在父项和子项中进行无闪烁绘图,并为子项提供透明度。这是我们得到的最终演示程序:

#include <Windows.h>

// the handle to the single child window
HWND child;

// parent's backbuffer so we can use it in the child
HDC bbuf = 0;
        
LRESULT CALLBACK WndProc(HWND window, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        return 0;
        
    case WM_MOUSEMOVE:
        InvalidateRect(window, NULL, true);
        
        return 0;
    
    case WM_PAINT: {
        PAINTSTRUCT ps;
        POINT mpos;
        GetCursorPos(&mpos);
        ScreenToClient(window, &mpos);
    
        BeginPaint(window, &ps);
        
        // create the backbuffer once
        bbuf = bbuf ? bbuf : CreateCompatibleDC(ps.hdc);
    
        // hardcoded size is the same in the CreateWindowEx call
        static auto backbmp = CreateCompatibleBitmap(ps.hdc, 400, 400);
    
        static auto unused = SelectObject(bbuf, backbmp);
    
        POINT points[2] = {
            { 0, 0 },
            { mpos.x, mpos.y }
        };
    
        // painting into bbuf
        // give ourselves a white background so we can see the wblack line
        SelectObject(bbuf, (HBRUSH)GetStockObject(WHITE_BRUSH));
        Rectangle(bbuf, 0, 0, 400, 400);
        SelectObject(bbuf, (HBRUSH)GetStockObject(BLACK_BRUSH));
        Polyline(bbuf, points, 2);

        // get the child to paint itself into our bbuf
        SendMessage(child, WM_PAINT, 0, 0);
    
        // and after the child has drawn, bitblt everything onto the screen
        BitBlt(ps.hdc, 0, 0, 400, 400, bbuf, 0, 0, SRCCOPY);
    
        EndPaint(window, &ps);
    
        return 0;
    }

    case WM_ERASEBKGND:
        return 0;
        
    case WM_NCDESTROY:
        PostQuitMessage(0);
        break;
    }
        
    return DefWindowProc(window, uMsg, wParam, lParam);
}
    
LRESULT CALLBACK ChildWndProc(HWND window, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        return 0;
    
    case WM_PAINT: {
        PAINTSTRUCT ps;
    
        BeginPaint(window, &ps);

        static auto backbuffer = CreateCompatibleDC(ps.hdc);
        static auto backbmp = CreateCompatibleBitmap(ps.hdc, 100, 50);

        static auto unused = SelectObject(backbuffer, backbmp);

        // copy the parent's stuff into our backbuffer (the parent has already drawn)
        BitBlt(backbuffer, 0, 0, 100, 50, bbuf, 50, 150, SRCCOPY);

        RECT r = { 0, 0, 50, 100 };

        // draw into our backbuffer
        SetBkMode(backbuffer, TRANSPARENT);
        SetTextColor(backbuffer, RGB(255, 0, 0));
        DrawText(backbuffer, "hello", 5, &r, DT_NOCLIP | DT_TABSTOP | DT_EXPANDTABS | DT_NOPREFIX);

        // bitblt our stuff into the parent's backbuffer
        BitBlt(bbuf, 50, 150, 100, 50, backbuffer, 0, 0, SRCCOPY);
    
        EndPaint(window, &ps);
    
        return 0;
    }
    
    case WM_ERASEBKGND:
        return 0;
    }
        
    return DefWindowProc(window, uMsg, wParam, lParam);
}
    
int PASCAL WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int nShowCmd) {
    WNDCLASS wc;
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hinst;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "test_window";
        
    RegisterClass(&wc);
    
    wc.style         = 0;
    wc.lpfnWndProc   = ChildWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hinst;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "transparent_window";
        
    RegisterClass(&wc);
        
    HWND wnd = CreateWindowEx(NULL, "test_window", "test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 50, 50, 400, 400, NULL, NULL, hinst, NULL);
        
    child = CreateWindowEx(NULL, "transparent_window", "", WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CHILD, 50, 150, 100, 50, wnd, NULL, hinst, NULL);
    
    MSG m;
    
    while (GetMessage(&m, NULL, 0, 0)) {
        TranslateMessage(&m);
        DispatchMessage(&m);
    }
        
    return 0;
}

再次感谢 johnathon 花了这么多时间帮助我解决这个问题。

4

1 回答 1

2

从您的父窗口窗口样式中删除 WS_CLIPCHILDREN。这将允许父窗口在子窗口的不动产上进行绘制,然后子窗口将在其绘制调用期间有效地在其上绘制。首先调用父窗口的绘制,然后调用任何子窗口的绘制。祝赛斯好运!

于 2012-05-15T06:21:38.847 回答