SetCursorPos不起作用

6
我正在使用以下库:http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library?fid=1518257&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&fr=51#xx0xx,它与低级 Windows 7 鼠标钩子一起使用。我创建了一个计时器来检查最后一次鼠标移动事件发生的时间,如果超过给定时间,我就会使用 SetCursorPos(0,0) 将鼠标移到屏幕左上角。
在移动鼠标之前,我获取了它的旧坐标并保存了起来。因此,当我接收到下一个MouseMove事件时,我可以将鼠标放回原来的位置。但是,在调用SetCursorPos(oldPos.x, oldPos.y)之后,鼠标没有移动。
我确信oldPos的值是正确的,但是光标拒绝移动。这可能是由于我使用的库引起的吗?请帮忙看看。
[DllImport("user32.dll", SetLastError = true)]
    public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);
void mouseHook_MouseMove(object sender, MouseEventArgs e)
    {
        //If the mouse was not visible, move it back to it's original position
        if (!mouseVisible)
        {
            mouseVisible = true;

            SetCursorPos(cursorPosition.x, cursorPosition.y);
        }

        //Update the last moved time.
        lastMoved = DateTime.Now;
    }

private void hideMouse(object sender, EventArgs e)
    {
        if (mouseVisible && (DateTime.Now - lastMoved) > new TimeSpan(0, 0, 0, mouseControl.timeTrackBar.Value))
        {
            log.Debug("Hiding mouse.");

            //Store the current mouse position.
            GetCursorPos(out cursorPosition);

            //Hide the mouse.
            SetCursorPos(0, 0);
            log.Debug("Moving cursor to 0,0");

            mouseVisible = false;
        }

1
尝试发布一些代码。例如,您如何获取鼠标坐标? - Adriano Repetti
@dowhilefor 你有什么办法可以画出全局的小圆点吗?或者在这个事件之后如何取消后续的鼠标移动事件?我查看了一下"Handled"属性,但MouseEventArgs没有这个属性。 - DTI-Matt
如果您需要取消(或更改)鼠标事件,最好使用钩子本身。您在钩子过程中获得的结构可以修改以更改其他应用程序将接收的事件。 - Adriano Repetti
为什么不直接使用 Cursor 类呢? - Ceramic Pot
@CeramicPot 因为那只会影响到我的应用程序表面上的光标,而不是全局性的。 - DTI-Matt
显示剩余5条评论
1个回答

1

我的猜测是这样的:

  1. 您使用SetCursor将鼠标移动到0,0。
  2. 调用SetCursor会从您的钩子生成鼠标移动事件。
  3. 您通过显示光标并将其放回原来的位置来响应鼠标移动。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接