鼠标在外部C#代码中移动到游戏窗口

时间:2015-07-20 10:30:41

标签: c# windows winforms dllimport

我正在玩游戏,每当我需要移动鼠标并点击进入游戏屏幕以获得游戏点时,我就会尝试用c#编写代码来点击并移动游戏中的鼠标' s屏幕自动.. 我得到了一些帮助,鼠标点击问题解决了,但是我无法在游戏的屏幕上移动鼠标。 你能告诉我该怎么办? 我应该使用" 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));

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

1 个答案:

答案 0 :(得分:2)

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

Move(830, 160);

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

class General

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