I used the following code but got an error
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class MouseHelper {
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
public int X;
public int Y;
}
}
"@
function Get-CursorPos {
$point = [MouseHelper]::POINT{}
[MouseHelper]::GetCursorPos([ref]$point) | Out-Null
return $point
}
function Drag-And-Drop {
param(
[Parameter(Mandatory=$true)]
[int]$startX,
[Parameter(Mandatory=$true)]
[int]$startY,
[Parameter(Mandatory=$true)]
[int]$endX,
[Parameter(Mandatory=$true)]
[int]$endY
)
$cursorPos = Get-CursorPos
$currentX = $cursorPos.X
$currentY = $cursorPos.Y
[MouseHelper]::SetCursorPos($startX, $startY)
[MouseHelper]::mouse_event(0x0002, 0, 0, 0, 0)
[MouseHelper]::SetCursorPos($endX, $endY)
[MouseHelper]::mouse_event(0x0004, 0, 0, 0, 0)
[MouseHelper]::SetCursorPos($currentX, $currentY)
}
Drag-And-Drop -startX 100 -startY 100 -endX 200 -endY 200
The following is the error message
The method call failed because [MouseHelper] does not contain a method named "POINT". Location C:UsersKKKKpowercat-mastershubiao.ps1:24 Char: 5
$point = [MouseHelper]::POINT{}
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : MethodNotFound
Cannot apply [ref] to a variable that does not exist. Location C:UsersKKKKpowercat-mastershubiao.ps1:25 Characters: 5
[MouseHelper]::GetCursorPos([ref]$point) | Out-Null
~~ ...
I hope to be able to run the simulated mouse click correctly, and I can use other code.
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
“It looks like your post is mostly code; please add some more details.”
Oh, I am speechless. I have almost finished adding
ONNN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.