如何在Windows中从鼠标钩子设置光标位置? [英] How can I set cursor position from mouse hook in Windows?

查看:264
本文介绍了如何在Windows中从鼠标钩子设置光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我谈到C ++的时候,我还是很新手,所以和我一起。我试图创建一个小应用程序,将检测何时鼠标光标移动到屏幕的边缘,并将其移动到相反的边缘,创建一个连续的桌面效果,如果这是有道理的。

I'm fairly novice when it comes to C++, so bare with me. I'm trying to create a little app that will detect when the mouse cursor moves to the edge of the screen and will move it to the opposite edge, to create a continuous desktop effect, if that makes sense.

下面是来自别人的一些代码(鼠标钩子部分)我通过添加SetCursorPos来移动鼠标到一个固定的位置。当我运行它,SetCursorPos返回true,我认为意味着调用成功,但鼠标不移动。我读某处关于安全约束在后来的Windows版本阻止这样的东西,这将是有意义的,但源头不清楚这是多么真实。有没有人知道为什么这样做不起作用?

Below is some code from someone else (the mouse hook part) I adapted by added the SetCursorPos to move the mouse to a fixed position for now. When I run it, SetCursorPos returns true, which I assume means the call succeeded, but the mouse does not move. I read somewhere something about security constraints in later Windows version preventing stuff like this, which would make sense, but the source was unclear to how true this is. Does anyone know why this would not work?

谢谢,代码如下:

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


推荐答案

这似乎是一个问题在你的hook过程中调用SetCursorPos。我想这是明确禁止在Vista / Windows 7,但我找不到任何文件来确认。我稍微修改你的代码以发布一个线程消息,当它想移动光标,并做你的消息过程中的实际SetCursorPos。

This appears to be a problem with calling SetCursorPos inside your hook proceedure. I guess this is explicitly prohibted in Vista/Windows 7, but I couldn't find any documentation to confirm that. I modified your code slightly to post a thread message when it wants to move the cursor, and do the actual SetCursorPos inside your message proceedure. It works fine once this is done.

在您的hook过程中:

In your hook procedure:

if (pMouseStruct->pt.x < -1900)
    {
        PostThreadMessage( GetCurrentThreadId(), WM_USER, 0, 0 );
        printf("Trigger %d.  Response %d", pMouseStruct->pt.x, r);
    }

在您的邮件循环中:

while (GetMessage(&message,NULL,0,0)) {
    if( message.hwnd == NULL ) {
        if( message.message == WM_USER ) {
            SetWindowPos( 500, 500 );
        }
     } else {
         TranslateMessage( &message );
         DispatchMessage( &message );
     }
}

(注意这只是一个演示,修复。)

(Note this is just a demonstration, not an actual fix.)

话虽如此,你的代码有很多严重的问题。我不认为这里适合所有人,但我建议您在 http://codereview.stackexchange.com/

That being said, there are numerous, serious problems with your code. I don't think it's appropriate to go into all of them here, but I recommend you post it on http://codereview.stackexchange.com/.

这篇关于如何在Windows中从鼠标钩子设置光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆