WinAPI CreateWindow WS_POPUP resize flickering

897 Views Asked by At

I'm creating a window with no borders (WS_POPUP in CreateWindow) and need to resize it when mouse is over and change back when mouse is out of the application. It works OK, but it flickers when I change the position using SetWindowPos. I need to decrease y by 30 pixels and increase height, so the bottom of the window remains in the same position.

Below is the code:

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;
}

I tried all combinations of SetWindowPos, MoveWindow etc but when mouse is over the window, it seems that window is first settings Y position, then it increases the height after a few milliseconds and this is visible.

Does anyone know a solution for this?

1

There are 1 best solutions below

1
On

I fixed the problem by using the SetWindowRgn function. This still doesn't answer the question why flickering happens, but it fixed the problem for me as I was able to achieve the same thing. Code is below:

HRGN region = CreateRectRgn(0, BUTTONS_AREA_HEIGHT, width, height);
SetWindowRgn(windowHandle, region, TRUE);
DeleteObject(region);