为了帮助我在 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;
}