通过发送消息单击鼠标 [英] Clicking mouse by sending messages

查看:116
本文介绍了通过发送消息单击鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将鼠标点击发送给程序.因为我不希望鼠标移动,所以我不想使用SendInput或mouse_event,并且由于应该接收点击的窗口并没有真正使用Buttons或其他GUI事件,所以我无法向这些消息发送消息按钮.

I'm trying to send mouse clicks to a program. As I don't want the mouse to move, I don't want to use SendInput or mouse_event, and because the window that should receive the clicks doesn't really use Buttons or other GUI events, I can't send messages to these buttons.

我正在尝试使用SendMessage使其正常工作,但是由于某些原因,它不起作用.相关代码是(在C#中,但也在jnative上尝试过Java),在Vista上尝试过

I'm trying to get this working using SendMessage, but for some reason it doesn't work. Relevant code is (in C#, but tried Java with jnative as well), trying this on Vista

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern int SendMessage(IntPtr A_0, int A_1, int A_2,  int  A_3);

    static int WM_CLOSE = 0x10;
    static int WM_LBUTTONDOWN = 0x201;
    static int WM_LBUTTONUP = 0x202;

    public static void click(IntPtr hWnd, int x, int y)
    {
        SendMessage(hWnd, WM_LBUTTONDOWN, 1, ((x << 0x10) ^ y));
        SendMessage(hWnd, WM_LBUTTONUP, 0, ((x << 0x10) ^ y));
    }

    public static void close(IntPtr hWnd)
    {
        SendMessage(hWnd, WM_CLOSE, 0, 0);
    }

close可以正常工作,但是click却什么也没做.

The close works fine, but the click doesn't do anything.

发现了问题.除了如下所述的替换x和y坐标的愚蠢错误外,我没有检查接收点击的Window句柄是否也是正确的客户端窗口.我现在有

edit: Found the problem. Besides the stupid bug to replace the x and y coordinates, as suggested below, I didn't check if the Window handle that receives the click is also the correct client window. I now have

        POINT p = new POINT(x, y);
        IntPtr hWnd = WindowFromPoint(p);

        RECT rct = new RECT();

        if (!GetWindowRect(hWnd, ref rct))
        {
            return;
        }

        int newx = x - rct.Left;
        int newy = y - rct.Top;
        SendMessage(hWnd, WM_LBUTTONDOWN, 1, newy * 65536 + newx);
        SendMessage(hWnd, WM_LBUTTONUP, 0, newy * 65536 + newx);

效果很好.

推荐答案

问题在于您打包x,y坐标.

The problem is with your packing of the x,y coordinates.

  1. y应该是高阶词
  2. 您应该使用| (按位或)以组合坐标对的组成部分
  1. y should be in the high order word
  2. You should use | (bitwise or) to combine the components of the coordinate pair

您应该具备以下条件

((y << 0x10) | x)

这篇关于通过发送消息单击鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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