English 中文(简体)
我如何使用自动化,以安装视窗7的模拟设备进行右侧扫描?
原标题:How can I use automation to right-click with a mouse in Windows 7?

我先花了一段时间,然后才能在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;
    }
}

视窗的这项工作 7. 为什么不,我如何确定?

为了了解更多情况,Im利用这一工具,以形成一种环境菜单。

Edit: @GSerg 链接看上去是有用的。 我不敢肯定,一旦你加入工会,投入的“类型”领域会发生什么,但我留下了空白,现在,这造成我的筛选即将到来。 Ah, Win32的喜悦。 感谢任何帮助。

最佳回答

在此,我试图找到解决办法——这一试验方案结合了开放问题中的代码、平伏克净投入页和

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

}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签