Powershell用于鼠标左键单击并拖动到新屏幕坐标的功能

时间:2018-02-14 17:34:19

标签: powershell automation

我希望有一个Powershell功能,可以执行以下操作:

  1. 鼠标左键单击并在屏幕坐标(x,y)处单击并保持
  2. 鼠标左键单击按钮移动到新屏幕坐标(x + 800,y + 1000)
  3. 鼠标左键单击新屏幕坐标的释放
  4. 我正在处理下面帖子中发现的代码(由StephenP发布),但我对其他实现此自动化任务的方法持开放态度:

    How i can send mouse click in powershell

1 个答案:

答案 0 :(得分:2)

根据引用问题中的代码,您可以执行以下操作:

$cSource = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Dragger
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{ 
    public int        type; // 0 = INPUT_MOUSE,
                            // 1 = INPUT_KEYBOARD
                            // 2 = INPUT_HARDWARE
    public MOUSEINPUT mi;
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
    public int    dx ;
    public int    dy ;
    public int    mouseData ;
    public int    dwFlags;
    public int    time;
    public IntPtr dwExtraInfo;
}

//This covers most use cases although complex mice may have additional buttons
//There are additional constants you can use for those cases, see the msdn page
const int MOUSEEVENTF_MOVED      = 0x0001 ;
const int MOUSEEVENTF_LEFTDOWN   = 0x0002 ;
const int MOUSEEVENTF_LEFTUP     = 0x0004 ;
const int MOUSEEVENTF_RIGHTDOWN  = 0x0008 ;
const int MOUSEEVENTF_RIGHTUP    = 0x0010 ;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
const int MOUSEEVENTF_MIDDLEUP   = 0x0040 ;
const int MOUSEEVENTF_WHEEL      = 0x0080 ;
const int MOUSEEVENTF_XDOWN      = 0x0100 ;
const int MOUSEEVENTF_XUP        = 0x0200 ;
const int MOUSEEVENTF_ABSOLUTE   = 0x8000 ;

const int screen_length = 0x10000 ;

//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

public static void Drag(int x1, int y1, int x2, int y2)
{
    //Move the mouse
    INPUT[] input = new INPUT[4];
    input[0].mi.dx = x1*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    input[0].mi.dy = y1*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    //Left mouse button down
    input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    // Mouse move
    input[2].mi.dx = x2*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    input[2].mi.dy = y2*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    input[2].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    //Left mouse button up
    input[3].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(4, input, Marshal.SizeOf(input[0]));
}
}
'@
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
#Send drag from one point to another
[Dragger]::Drag(50, 20, 200, 100)

你想要的不是左键单击,而是左键单击鼠标,然后是鼠标移动和鼠标向上。上面的例子拖动了一个从50,20到200,100的窗口(只要有一个窗口)。只需输入您自己的坐标,就可以了。