1

我正在创建一个没有边框的窗口(CreateWindow 中的 WS_POPUP),并且需要在鼠标悬停时调整它的大小,并在鼠标离开应用程序时更改回来。它工作正常,但是当我使用 SetWindowPos 更改位置时它会闪烁。我需要将 y 减少 30 像素并增加高度,所以窗口的底部保持在相同的位置。

下面是代码:

WNDCLASS windowClass;
ZeroMemory(&windowClass, sizeof(WNDCLASS));

windowClass.style = 0;// CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = (WNDPROC) WindowProc;
windowClass.hInstance = hInst;
windowClass.lpszClassName = PROCESS_WINDOW_CLASS;
windowClass.hIcon = NULL;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);

windowHandle = CreateWindowEx(
    0,
    PROCESS_WINDOW_CLASS,
    L"",
    WS_POPUP | WS_CLIPSIBLINGS,
    x,  //x
    y,  //y
    width, //width
    height, //height
    NULL, 
    NULL, 
    hInst,
    NULL);

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
    case WM_MOUSEMOVE: {
        if (!mouseOver) {
            TRACKMOUSEEVENT tme;

            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_LEAVE;
            tme.hwndTrack = hwnd;
            if (TrackMouseEvent(&tme)) {
                RECT rect;
                GetWindowRect(hwnd, &rect);

                int y = rect.top;
                int height = rect.bottom - y;

                y -= BUTTONS_AREA_HEIGHT;
                height += BUTTONS_AREA_HEIGHT;
                //tried with MoveWindow, DeferWindowPos - same thing
                //MoveWindow(hwnd, rect.left, y, rect.right - rect.left, height, TRUE);

                //HDWP winPos = BeginDeferWindowPos(1);
                //DeferWindowPos(winPos, hwnd, NULL, rect.left, y, rect.right - rect.left, height, 0);
                //EndDeferWindowPos(winPos);

                SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height,
                    SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW | SWP_NOSENDCHANGING);
                rect.top = y;
                rect.bottom = height - y;
                InvalidateRect(hwnd, &rect, FALSE);
                    //0);
                RedrawWindow(hwnd, NULL, NULL, RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_NOERASE);
                mouseOver = TRUE;
            }
        }
    }
    break;
    case WM_MOUSELEAVE: {
        RECT rect;
        GetWindowRect(hwnd, &rect);

        int y = rect.top;
        int height = rect.bottom - y;

        y += BUTTONS_AREA_HEIGHT;
        height -= BUTTONS_AREA_HEIGHT;

        MoveWindow(hwnd, rect.left, y, rect.right - rect.left, height, TRUE);

        //SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height,
        //  SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
        //InvalidateRect(hwnd, &rect, FALSE);
        //SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height, 0);
        mouseOver = FALSE;
    }
    case WM_CREATE: {
        LONG lExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
        lExStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | 
            WS_SYSMENU | WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);

        SetWindowLongPtr(hwnd, GWL_EXSTYLE, lExStyle | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);

        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    }
        return 0;
    case WM_DESTROY:
        PostQuitMessage(EXIT_SUCCESS);
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}

我尝试了 SetWindowPos、MoveWindow 等的所有组合,但是当鼠标悬停在窗口上时,似乎窗口首先设置了 Y 位置,然后它在几毫秒后增加了高度,这是可见的。

有谁知道这个的解决方案?

4

1 回答 1

0

我通过使用 SetWindowRgn 函数解决了这个问题。这仍然不能回答为什么会发生闪烁的问题,但它为我解决了这个问题,因为我能够实现同样的事情。代码如下:

HRGN region = CreateRectRgn(0, BUTTONS_AREA_HEIGHT, width, height);
SetWindowRgn(windowHandle, region, TRUE);
DeleteObject(region);
于 2016-10-31T10:40:36.933 回答