gpt4 book ai didi

c# - 通过外部 C# 代码在游戏窗口中移动鼠标

转载 作者:可可西里 更新时间:2023-11-01 10:19:56 28 4
gpt4 key购买 nike

我在玩游戏,每次我需要移动鼠标并点击进入游戏屏幕来获得游戏点数,所以我尝试用 C# 编写代码来自动点击并在游戏屏幕中移动鼠标..我得到了一些帮助,所以鼠标点击问题解决了,但我无法在游戏屏幕上移动鼠标。你能告诉我我该怎么做吗??我应该使用“SetWindowsHookEx”还是其他方法在游戏窗口中移动鼠标??请告诉我我该怎么做..

我得到的下面的“点击”代码,它工作正常:

public class ClickGameScreen
{

[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

[DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);

internal struct INPUT
{
public UInt32 Type;
public MOUSEKEYBDHARDWAREINPUT Data;
}

[StructLayout(LayoutKind.Explicit)]
internal struct MOUSEKEYBDHARDWAREINPUT
{
[FieldOffset(0)]
public MOUSEINPUT Mouse;
}

internal struct MOUSEINPUT
{
public Int32 X;
public Int32 Y;
public UInt32 MouseData;
public UInt32 Flags;
public UInt32 Time;
public IntPtr ExtraInfo;
}

public static void ClickScreen(IntPtr W_Handle , Point C_Point)
{
var oldPos = Cursor.Position;
ClientToScreen(W_Handle, ref C_Point);
Cursor.Position = new Point(C_Point.X, C_Point.Y);

var inputDown = new INPUT();
inputDown.Type = 0;
inputDown.Data.Mouse.Flags = 0x0002;

var inputUp = new INPUT();
inputUp.Type = 0;
inputUp.Data.Mouse.Flags = 0x0004;

var inputs = new INPUT[] { inputDown, inputUp };
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

Cursor.Position = oldPos;
}

}

==============

ClickScreen(this.Handle, new Point(375, 340));

===============

最佳答案

我解决了我的问题,代码如下:

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

private const int MOUSEEVENTF_MOVE = 0x0001;

public static void Move(int xDelta, int yDelta)
{
mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
}

//=========================================

Move(830, 160);

//=========================================

关于c# - 通过外部 C# 代码在游戏窗口中移动鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31514213/

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