单击指定的客户区域

时间:2012-11-25 04:48:14

标签: c# click client mouse sendmessage

Click不起作用 - 我不知道为什么也无法找到解决方案:(

即。 Click(150,215)应将鼠标移动到客户区并单击此处。

    [DllImport("user32.dll")]
    private static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);

    [DllImport("user32", SetLastError = true)]
    private static extern int SetCursorPos(int x, int y);

    static void MouseMove(int x, int y)
    {
        Point p = new Point(x * -1, y * -1);
        ScreenToClient(hWnd, ref p);
        p = new Point(p.X * -1, p.Y * -1);
        SetCursorPos(p.X, p.Y);
    }

    static void Click(int x, int y)
    {
        MouseMove(x, y);
        SendMessage(hWnd, WM_LBUTTONDOWN, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
        SendMessage(hWnd, WM_LBUTTONUP, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
    }

修改 以下方法有效,但我会说“更先进”。

当然我可以使用mouse_event,但我希望看到SendMessage()的解决方案......

    [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    const int LEFTDOWN = 0x00000002;
    const int LEFTUP = 0x00000004;

    static void Click(int x, int y)
    {
        MouseMove(x, y);
        mouse_event((int)(LEFTDOWN), 0, 0, 0, 0);
        mouse_event((int)(LEFTUP), 0, 0, 0, 0);
    }

过了一段时间后,我发现MouseInput的另一种方法是SendInput,但它很长:P

可以缩小吗?

        [Flags]
    enum MouseEventFlags : uint
    {
        MOUSEEVENTF_MOVE = 0x0001,
        MOUSEEVENTF_LEFTDOWN = 0x0002,
        MOUSEEVENTF_LEFTUP = 0x0004,
        MOUSEEVENTF_RIGHTDOWN = 0x0008,
        MOUSEEVENTF_RIGHTUP = 0x0010,
        MOUSEEVENTF_MIDDLEDOWN = 0x0020,
        MOUSEEVENTF_MIDDLEUP = 0x0040,
        MOUSEEVENTF_XDOWN = 0x0080,
        MOUSEEVENTF_XUP = 0x0100,
        MOUSEEVENTF_WHEEL = 0x0800,
        MOUSEEVENTF_VIRTUALDESK = 0x4000,
        MOUSEEVENTF_ABSOLUTE = 0x8000
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

    [StructLayout(LayoutKind.Explicit)]
    struct MouseKeybdhardwareInputUnion
    {
        [FieldOffset(0)]
        public MouseInputData mi;
    }
    struct MouseInputData
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public MouseEventFlags dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    struct INPUT
    {
        public SendInputEventType type;
        public MouseKeybdhardwareInputUnion mkhi;
    }
    enum SendInputEventType : int
    {
        InputMouse,
    }

    static void Click(int x, int y)
    {
        INPUT mouseInput = new INPUT();
        mouseInput.type = SendInputEventType.InputMouse;
        mouseInput.mkhi.mi.dx = x;
        mouseInput.mkhi.mi.dy = y;
        mouseInput.mkhi.mi.mouseData = 0;

        MouseMove(x, y);
        mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
        SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));

        mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
        SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
    }

1 个答案:

答案 0 :(得分:1)

虽然我没有调查您所描述的确切问题,但我建议您查看mouse_eventSendInput这些Windows API调用是为了执行您所描述的操作。

mouse_event是旧版本的通话,SendInput更新,并通过支持多个输入事件取代mouse_input。这两种方法都可以在P / Invoke.net上找到,网址如下: