我正在尝试创建一个小应用程序,该应用程序将检测鼠标光标何时移动到屏幕边缘并将其移动到另一边缘,以创建连续的桌面效果,如果这有意义的话。
下面是来自其他人的一些代码(鼠标钩部分),我通过添加 SetCursorPos 来将鼠标移动到固定位置。当我运行它时,SetCursorPos 返回 true,我认为这意味着调用成功,但鼠标没有移动。我在某个地方读到了一些关于更高版本 Windows 版本中的安全约束的内容,以防止这样的事情,这是有道理的,但来源不清楚这是多么真实。有谁知道为什么这不起作用?
谢谢,代码如下:
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL)
{
if (pMouseStruct->pt.x < -1900)
{
BOOL r = SetCursorPos(
500,
500
);
printf("Trigger %d. Response %d", pMouseStruct->pt.x, r);
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hMouseHook = SetWindowsHookEx (
WH_MOUSE_LL,
(HOOKPROC) KeyboardEvent,
hInstance,
NULL
);
MessageLoop();
UnhookWindowsHookEx(hMouseHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}