移动/滑动屏幕的子窗口导致更多的CPU使用

时间:2018-02-19 03:30:03

标签: c++ winapi

我正在尝试创建一个从主窗口侧面滑出的侧工具栏窗口。

我遇到的问题是,当它滑入(隐藏)它稍微落后并且稍微慢一点,当窗口滑出时,也会导致cpu跳得更多。

当工具栏滑出时,它没有这个问题。

这里是滑动窗口的所有代码。

int ControllerToolbar::mouseMove(WPARAM state, int x, int y)
{
    // for tracking mouse hover/leave tracking
    if (!mouseHovered)
    {
        slideWindow(1);
        // mouse hover/leave tracking
        TRACKMOUSEEVENT tme;
        tme.cbSize = sizeof(tme);
        tme.dwFlags = TME_HOVER | TME_LEAVE;
        tme.hwndTrack = handle;
        tme.dwHoverTime = HOVER_DEFAULT;
        ::TrackMouseEvent(&tme);
        mouseHovered = true;
    }

    if (state == MK_LBUTTON)
    {
    }
    if (state == MK_RBUTTON)
    {
    }

    return 0;
}


int ControllerToolbar::mouseHover(int state, int x, int y)
{
    mouseHovered = true;
    return 0;
}


int ControllerToolbar::mouseLeave()
{
    mouseHovered = false;
    slideWindow(2);
    return 0;
}


void ControllerToolbar::GetToolbarPos(int *x, int *y) 
{
    RECT rect = { NULL };
    if (GetWindowRect(handle, &rect)) {

        MapWindowPoints(HWND_DESKTOP, GetParent(handle), (LPPOINT)&rect, 2);

        *x = rect.left;
        *y = rect.top;
    }
}


void ControllerToolbar::slideWindow(int dir)
{
    std::thread([this, dir]
    {
        int x, y;
        GetToolbarPos(&x, &y);

        if (dir == 1)
        {
            while (x < 0 && mouseHovered)
            {
                x += 2;

                std::this_thread::sleep_for(std::chrono::milliseconds(1));

                if (x > 0)
                    ::SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
                else
                    ::SetWindowPos(handle, 0, x, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
            }
        }
        else if (dir == 2)
        {
            while (x > -90 && !mouseHovered)
            {
                x -= 2;

                std::this_thread::sleep_for(std::chrono::milliseconds(1));

                if (x < -90)
                    ::SetWindowPos(handle, 0, -90, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
                else
                    ::SetWindowPos(handle, 0, x, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);

            }
        }
    }
    ).detach();
}

0 个答案:

没有答案