gpt4 book ai didi

c# - 你如何在C#中模拟鼠标点击?

转载 作者:IT王子 更新时间:2023-10-29 03:34:48 26 4
gpt4 key购买 nike

如何在 C# winforms 应用程序中模拟鼠标点击?

最佳答案

我结合了几个来源来生成下面的代码,我目前正在使用它。我还删除了 Windows.Forms 引用,这样我就可以在控制台和 WPF 应用程序中使用它而无需其他引用。

using System;
using System.Runtime.InteropServices;

public class MouseOperations
{
[Flags]
public enum MouseEventFlags
{
LeftDown = 0x00000002,
LeftUp = 0x00000004,
MiddleDown = 0x00000020,
MiddleUp = 0x00000040,
Move = 0x00000001,
Absolute = 0x00008000,
RightDown = 0x00000008,
RightUp = 0x00000010
}

[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetCursorPos(int x, int y);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePoint lpMousePoint);

[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

public static void SetCursorPosition(int x, int y)
{
SetCursorPos(x, y);
}

public static void SetCursorPosition(MousePoint point)
{
SetCursorPos(point.X, point.Y);
}

public static MousePoint GetCursorPosition()
{
MousePoint currentMousePoint;
var gotPoint = GetCursorPos(out currentMousePoint);
if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
return currentMousePoint;
}

public static void MouseEvent(MouseEventFlags value)
{
MousePoint position = GetCursorPosition();

mouse_event
((int)value,
position.X,
position.Y,
0,
0)
;
}

[StructLayout(LayoutKind.Sequential)]
public struct MousePoint
{
public int X;
public int Y;

public MousePoint(int x, int y)
{
X = x;
Y = y;
}
}
}

关于c# - 你如何在C#中模拟鼠标点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2416748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com