Win32:测试鼠标是否悬停了一秒

时间:2015-08-23 21:39:24

标签: c++ winapi callback mouse

我正在尝试创建一个只在鼠标停留在相同位置时才运行的函数,而不会使程序停止(因此没有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;
            }

        }

当您移动鼠标并单击时,会生成随机闪烁。任何洞察我做错的事情都会受到赞赏。

0 个答案:

没有答案