鼠标通过外部C#代码在游戏窗口中移动 [英] Mouse move in game window by outer C# code
问题描述
我在玩游戏,每次我需要移动鼠标并单击进入游戏屏幕以获取游戏积分时,所以我试图用c#编写代码以自动单击并移动游戏屏幕中的鼠标..
我得到了一些帮助,所以鼠标单击问题得以解决,但是我无法在游戏屏幕上移动鼠标。.
您能否建议我该怎么办?
我应该使用 SetWindowsHookEx或其他方法在游戏窗口中移动鼠标吗?
请告诉我该怎么办。
I am playing a game, every time I need to move mouse and click into the game's screen to earn game point, so I am trying to write code by c# to click and move mouse in game's screen automatically .. I got some help, so mouse click problems solved, but I am unable to move mouse in game's screen.. Can you please advice me what should I do ?? Should I use "SetWindowsHookEx" or other methods to move mouse in game window ?? Please advice me what should I do..
在下面得到的点击代码,它工作正常:
"Click" code below which I got, it's working fine :
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));
=============
===============
推荐答案
我解决了我的问题,代码如下:
I solved my problem, code below :
[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#代码在游戏窗口中移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!