我正在尝试创建一个函数,该函数仅在鼠标在同一位置停留一段时间后才运行,而不会停止程序(因此没有 Sleep(1000) 函数)。我试图做的是让回调在鼠标移动时在绘画中运行一个函数。在该函数中,将有一个 while 循环来确保鼠标自上一帧以来没有移动,并在单击或移动时中断。

在回调中:

case WM_MOUSEMOVE:
    POINT pt;
    GetCursorPos(&pt);
    ScreenToClient(hWnd, &pt);
    iPosX = pt.x;
    iPosY = pt.y;

    if (!mouseMoved) {
        mouseMoved = true;
        period = 0;
        InvalidateRect(hWnd, 0, FALSE);
    }
    break;

在油漆中:
       if (mouseMoved && !MouseClicked) {

            test.newBit(1, 100, 0, hdc); //Draws a picture as a test

            oldx = iPosX;
            oldy = iPosY;

            period = 0;

            while (period <= 1000000 && !MouseClicked) {

                oldx = iPosX;
                oldy = iPosY;

                POINT pt;
                GetCursorPos(&pt);
                ScreenToClient(hWnd, &pt);
                iPosX = pt.x;
                iPosY = pt.y;

                if (iPosX != oldx || iPosY != oldy) {
                    mouseMoved = false;
                    period = 0;
                    break;
                }

                period++;
            }

            if (period <= 1000000) {
                period = 0;
                test.newBit(0, 0, 0, hdc); //Draws a picture as a test
                mouseMoved = false;
            }

        }

当您移动鼠标并单击时,这会产生随机闪烁。对我做错了什么的任何见解将不胜感激。

最佳答案

系统已经为您实现了这一点。你只需要通过调用 TrackMouseEvent 来请求鼠标悬停消息:

TRACKMOUSEEVENT tme = { sizeof( tme ) };
tme.dwFlags = TME_HOVER;
tme.hwndTrack = hWnd;
tme.dwHoverTime = 1000;
::TrackMouseEvent( &tme );

当光标悬停在窗口的客户区上达 TrackMouseEvent 调用中指定的时间段时,应用程序会收到 WM_MOUSEHOVER 消息。一旦生成 WM_MOUSEHOVER 消息,悬停跟踪就会停止。要接收更多 WM_MOUSEHOVER 消息,应用程序需要重新请求悬停跟踪(使用与上述相同的代码)。

应用程序可以调用 SystemParametersInfo 并使用 SPI_GETMOUSEHOVERTIME 来检索默认悬停超时。

请注意,这考虑了悬停矩形( SPI_GETMOUSEHOVERWIDTHSPI_GETMOUSEHOVERHEIGHT )。这是鼠标光标周围的区域,允许鼠标在其中移动,无需重置悬停超时。

如果您需要一个仅在鼠标根本没有移动时报告悬停的解决方案,则必须自己实现。基于计时器的解决方案是一个干净的解决方案。

每当鼠标移动时,我们需要重置计时器。如果鼠标之前在窗口的客户区之外,我们还必须重新请求 WM_MOUSELEAVE 消息。这是必要的,以便可以在鼠标离开客户区时取消计时器:
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
    enum TimerId { TimerId_MouseHover = 1 };
    static const UINT HoverTimeoutInMs = 1000;
    static int PrevX = INT_MIN;
    static int PrevY = INT_MIN;
    static bool IsMouseOutside = true;
    static bool IsMouseHovered = false;

    switch ( message ) {

    case WM_MOUSEMOVE: {
        // If mouse was previously outside, re-request WM_MOUSELEAVE messages
        if ( IsMouseOutside ) {
            TRACKMOUSEEVENT tme = { sizeof( tme ) };
            tme.dwFlags = TME_LEAVE;
            tme.hwndTrack = hWnd;
            ::TrackMouseEvent( &tme );
            IsMouseOutside = false;
        }

        int CurrX = GET_X_LPARAM( lParam );
        int CurrY = GET_Y_LPARAM( lParam );
        if ( ( CurrX != PrevX ) || ( CurrY != PrevY ) ) {
            // Mouse moved -> reset timer
            ::SetTimer( hWnd, TimerId_MouseHover, HoverTimeoutInMs, nullptr );
            PrevX = CurrX;
            PrevY = CurrY;
            IsMouseHovered = false;
            // For testing only:
            ::InvalidateRect( hWnd, nullptr, FALSE );
        }
        return 0;
    }

每当鼠标离开客户区时,我们都需要取消计时器。如果我们不取消计时器,它就会过期,即使鼠标移到窗口的客户区之外:
    case WM_MOUSELEAVE:
        ::KillTimer( hWnd, TimerId_MouseHover );
        IsMouseOutside = true;
        PrevX = INT_MIN;
        PrevY = INT_MIN;
        return 0;

如果我们的计时器到期,我们将有一个悬停事件:
    case WM_TIMER:
        if ( wParam == TimerId_MouseHover ) {
            // The mouse hasn't been moved for the specified timeout:
            // This is a hover event
            IsMouseHovered = true;
            // For testing only:
            ::InvalidateRect( hWnd, nullptr, FALSE );
            return 0;
        }
        else {
            return ::DefWindowProc( hWnd, message, wParam, lParam );
        }

作为测试,让我们用黑色画笔填充客户区,每当悬停超时到期时:
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint( hWnd, &ps );
        ::FillRect( hdc, &ps.rcPaint, GetStockBrush( IsMouseHovered ? BLACK_BRUSH :
                                                                      WHITE_BRUSH ) );
        EndPaint( hWnd, &ps );
        return 0;
    }

关于c++ - Win32 : Test if mouse has hovered for a second,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32171812/

10-13 00:04