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;
}