简体   繁体   English

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

[英]Powershell Function to left mouse click and drag to new screen coordinates

I am looking to have a Powershell function that does the following: 我正在寻找具有以下功能的Powershell功能:

  1. Mouse Left Clicks and stays clicked at screen coordinates (x, y) 鼠标左键单击并保持单击屏幕坐标(x,y)
  2. Mouse Moves, while left click button is held, to new screen coordinates (x+800, y+1000) 按住鼠标左键时,鼠标移动到新的屏幕坐标(x + 800,y + 1000)
  3. Mouse Left Click Releases at new screen coordinates 鼠标左键释放在新的屏幕坐标

I am working with the code found in the post below (Posted by StephenP) but am open to other methods of achieving this automation task: 我正在使用下面的帖子(由StephenP发布)中找到的代码,但是对实现此自动化任务的其他方法持开放态度:

How i can send mouse click in powershell 我如何在Powershell中发送鼠标单击

Building on the code in your referenced question, you can do this: 以您所引用问题中的代码为基础,可以执行以下操作:

$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)

What you want is not a left click, but a left mouse down, followed by a mouse move and a mouse up. 您想要的不是单击鼠标左键,而是按下鼠标左键,然后移动鼠标并向上移动鼠标。 The example above drags a window (provided there is one) from 50,20 to 200,100. 上面的示例将一个窗口(假设有一个窗口)从50,20拖动到200,100。 Just enter your own coordinates and you should be good to go. 只需输入您自己的坐标,您就应该做好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM