如何在 Windows 7 中使用鼠标右键单击自动化功能?

发布于 2024-12-27 09:56:12 字数 2049 浏览 5 评论 0原文

我花了一段时间才让我的鼠标能够在 Windows XP 中右键单击。代码如下:

public class Mouse
{
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern uint SendInput(uint numberOfInputs, Input[] inputs, int sizeOfInputStructure);

    private const int RightDown = 0x0008;
    private const int RightUp = 0x0010;
    private const int InputMouse = 0;

    public void RightClick<T>(T element) where T: AutomationElementWrapper
    {

        var point = element.Element.GetClickablePoint();
        var processId = element.Element.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
        var window = AutomationElement.RootElement.FindFirst(
            TreeScope.Children,
            new PropertyCondition(AutomationElement.ProcessIdProperty,
                                  processId));

        window.SetFocus();

         var x = (int)point.X;
         var y = (int)point.Y;

        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);

        SendInput(2, new[] {InputFor(RightDown, x, y), InputFor(RightUp, x, y)}, Marshal.SizeOf(typeof (Input)));
    }


    private static Input InputFor(uint mouseButtonAction, int x, int y)
    {
        var input = new Input
                        {
                            Dx = x,
                            Dy = y,
                            MouseData = 0,
                            DwFlags = mouseButtonAction,
                            Time = 0,
                            DwType = InputMouse,
                            MouseExtraInfo = new IntPtr()
                        };
        return input;
    }

    internal struct Input
    {
        public int DwType;
        public int Dx;
        public int Dy;
        public uint MouseData;
        public uint DwFlags;
        public uint Time;
        public IntPtr MouseExtraInfo;
    }
}

这在 Windows 7 中不起作用。为什么不行,如何修复它?

有关更多信息,我在自动化工具中使用它来调出上下文菜单。

编辑:@GSerg 的链接看起来很有帮助。不过,我不确定添加联合后输入的“类型”字段中会发生什么 - 我将其留空,现在这会导致我的屏幕保护程序启动。啊,Win32 的乐趣。任何帮助表示赞赏。

I've spent a while getting my mouse to be able to right-click in Windows XP. Here's the code:

public class Mouse
{
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern uint SendInput(uint numberOfInputs, Input[] inputs, int sizeOfInputStructure);

    private const int RightDown = 0x0008;
    private const int RightUp = 0x0010;
    private const int InputMouse = 0;

    public void RightClick<T>(T element) where T: AutomationElementWrapper
    {

        var point = element.Element.GetClickablePoint();
        var processId = element.Element.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
        var window = AutomationElement.RootElement.FindFirst(
            TreeScope.Children,
            new PropertyCondition(AutomationElement.ProcessIdProperty,
                                  processId));

        window.SetFocus();

         var x = (int)point.X;
         var y = (int)point.Y;

        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);

        SendInput(2, new[] {InputFor(RightDown, x, y), InputFor(RightUp, x, y)}, Marshal.SizeOf(typeof (Input)));
    }


    private static Input InputFor(uint mouseButtonAction, int x, int y)
    {
        var input = new Input
                        {
                            Dx = x,
                            Dy = y,
                            MouseData = 0,
                            DwFlags = mouseButtonAction,
                            Time = 0,
                            DwType = InputMouse,
                            MouseExtraInfo = new IntPtr()
                        };
        return input;
    }

    internal struct Input
    {
        public int DwType;
        public int Dx;
        public int Dy;
        public uint MouseData;
        public uint DwFlags;
        public uint Time;
        public IntPtr MouseExtraInfo;
    }
}

This doesn't work in Windows 7. Why not, and how do I fix it?

For more information, I'm using this in an automation tool to bring up a context menu.

Edit: @GSerg's link looks helpful. I'm not sure what goes in the "type" field of the input once you've added the union, though - I left it blank, and now this causes my screensaver to come on. Ah, the joys of Win32. Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2025-01-03 09:56:12

这是我尝试的解决方案 - 该测试程序结合了开头问题、pinvoke.net SendInput 页面和 oldnewthing 64 位结构对齐解决方法 由 GSerg 链接。

有了这个,右键单击对我来说在 Win7 x64 上有效:

using System;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using WiPFlash;
using WiPFlash.Components;
using WiPFlash.Framework;
using WiPFlash.Util;
using WiPFlash.Exceptions;

using NUnit.Framework;

namespace MouseRightClick
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = new ApplicationLauncher(TimeSpan.Parse("00:00:20"))
                .LaunchOrRecycle("foo", @"C:\\hg\\wipflash\\Example.PetShop\\bin\\Debug\\Example.PetShop.exe", Assert.Fail);

            var window = application.FindWindow("petShopWindow");
            var totalLabel = window.Find<Label>("copyPetContextTarget");
            Mouse mouse = new Mouse();
            mouse.RightClick(totalLabel);
        }
    }

    public class Mouse
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);

        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;
        private const int INPUT_MOUSE = 0;
        private const int INPUT_KEYBOARD = 1;
        private const int INPUT_HARDWARE = 2;

        public void RightClick<T>(T element) where T : AutomationElementWrapper
        {
            var point = element.Element.GetClickablePoint();
            var processId = element.Element.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
            var window = AutomationElement.RootElement.FindFirst(
                TreeScope.Children,
                new PropertyCondition(AutomationElement.ProcessIdProperty,
                                      processId));

            window.SetFocus();

            var x = (int)point.X;
            var y = (int)point.Y;

            System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);

            SendInput(2, new[] {
                InputFor(MOUSEEVENTF_RIGHTDOWN, x, y),
                InputFor(MOUSEEVENTF_RIGHTUP, x, y) },
                Marshal.SizeOf(typeof(INPUT)));
        }


        private static INPUT InputFor(uint mouseButtonAction, int x, int y)
        {
            var input = new INPUT();
            input.type = INPUT_MOUSE;
            input.u.mi.dwFlags = mouseButtonAction;
            input.u.mi.time = 0;
            input.u.mi.dwExtraInfo = IntPtr.Zero;            
            return input;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct HARDWAREINPUT
        {
            public uint uMsg;
            public ushort wParamL;
            public ushort wParamH;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct INPUT_UNION
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public KEYBDINPUT ki;
            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        };

        [StructLayout(LayoutKind.Sequential)]
        internal struct INPUT
        {
            public int type;
            public INPUT_UNION u;
        }
    }

}

Here's my attempt at a solution - this test program combines the code from the opening question, the pinvoke.net SendInput page, and the oldnewthing 64-bit structure alignment workaround linked to by GSerg.

With this, right-clicking works for me on Win7 x64:

using System;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using WiPFlash;
using WiPFlash.Components;
using WiPFlash.Framework;
using WiPFlash.Util;
using WiPFlash.Exceptions;

using NUnit.Framework;

namespace MouseRightClick
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = new ApplicationLauncher(TimeSpan.Parse("00:00:20"))
                .LaunchOrRecycle("foo", @"C:\\hg\\wipflash\\Example.PetShop\\bin\\Debug\\Example.PetShop.exe", Assert.Fail);

            var window = application.FindWindow("petShopWindow");
            var totalLabel = window.Find<Label>("copyPetContextTarget");
            Mouse mouse = new Mouse();
            mouse.RightClick(totalLabel);
        }
    }

    public class Mouse
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);

        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;
        private const int INPUT_MOUSE = 0;
        private const int INPUT_KEYBOARD = 1;
        private const int INPUT_HARDWARE = 2;

        public void RightClick<T>(T element) where T : AutomationElementWrapper
        {
            var point = element.Element.GetClickablePoint();
            var processId = element.Element.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
            var window = AutomationElement.RootElement.FindFirst(
                TreeScope.Children,
                new PropertyCondition(AutomationElement.ProcessIdProperty,
                                      processId));

            window.SetFocus();

            var x = (int)point.X;
            var y = (int)point.Y;

            System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);

            SendInput(2, new[] {
                InputFor(MOUSEEVENTF_RIGHTDOWN, x, y),
                InputFor(MOUSEEVENTF_RIGHTUP, x, y) },
                Marshal.SizeOf(typeof(INPUT)));
        }


        private static INPUT InputFor(uint mouseButtonAction, int x, int y)
        {
            var input = new INPUT();
            input.type = INPUT_MOUSE;
            input.u.mi.dwFlags = mouseButtonAction;
            input.u.mi.time = 0;
            input.u.mi.dwExtraInfo = IntPtr.Zero;            
            return input;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct HARDWAREINPUT
        {
            public uint uMsg;
            public ushort wParamL;
            public ushort wParamH;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct INPUT_UNION
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public KEYBDINPUT ki;
            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        };

        [StructLayout(LayoutKind.Sequential)]
        internal struct INPUT
        {
            public int type;
            public INPUT_UNION u;
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文