События мыши в WPF - PullRequest
       2

События мыши в WPF

1 голос
/ 01 февраля 2012

У меня есть небольшой класс, который использует Windows32 user32.dll для вызова SetMouseInput и вызова событий мыши.

В моей основной форме Xaml, если я передам значение leftClick = true в SendMouseInput в этом классе, он должен щелкнуть по текущей позиции.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace clicker
{
    internal struct MouseInput
    {
        public int X;
        public int Y;
        public uint MouseData;
        public uint Flags;
        public uint Time;
        public IntPtr ExtraInfo;
    }

    internal struct Input
    {
        public int Type;
        public MouseInput MouseInput;
    }

    public static class NativeMethods
    {
        public const int InputMouse = 0;

        public const int MouseEventMove      = 0x01;
        public const int MouseEventLeftDown  = 0x02;
        public const int MouseEventLeftUp    = 0x04;
        public const int MouseEventRightDown = 0x08;
        public const int MouseEventRightUp   = 0x10;
        public const int MouseEventAbsolute  = 0x8000;

        private static bool lastLeftDown;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint SendInput(uint numInputs, Input[] inputs, int size);

        public static void SendMouseInput(int positionX, int positionY, int maxX, int maxY, bool leftDown)
        {
            if(positionX > int.MaxValue)
                throw new ArgumentOutOfRangeException("positionX");
            if(positionY > int.MaxValue)
                throw new ArgumentOutOfRangeException("positionY");

            Input[] i = new Input[2];

            // move the mouse to the position specified
            i[0] = new Input();
            i[0].Type = InputMouse;
            i[0].MouseInput.X = (positionX * 65535) / maxX;
            i[0].MouseInput.Y = (positionY * 65535) / maxY;
            i[0].MouseInput.Flags = MouseEventAbsolute | MouseEventMove;

            // determine if we need to send a mouse down or mouse up event
            if(leftDown)
            {
                i[1] = new Input();
                i[1].Type = InputMouse;
                i[1].MouseInput.Flags = MouseEventLeftDown;
                            i[1].MouseInput.Flags = MouseEventLeftUp;
            }

            // send it
            uint result = SendInput(2, i, Marshal.SizeOf(i[0]));
            if(result == 0)
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }

Скорее всего, я ошибаюсь в части i[1].MouseInput.Flags = MouseEventLeftUp. Что нужно сделать, чтобы заставить MouseLeftUp работать?

...