【问题标题】:Another way to make the mouse click?另一种让鼠标点击的方法?
【发布时间】:2019-11-02 17:16:23
【问题描述】:

我正在制作一个 AI,并希望它能够充当玩家。我使用一些代码来点击鼠标,这很有效。然后我编写了一些代码让鼠标点击随机位置,但它停止工作。

我变了

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04; 

private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;

但它似乎没有改变任何东西,除了它按下的位置。 我还让它在 mouseDown 和 mouseUp 之间等待 0.5 秒,但是当我运行 rndPlay(); 时它仍然不起作用,当我尝试只做 press1(); 时它确实起作用了。

        public static void MoveTo(int x, int y)
        {
            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
        }

        public static void LeftClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y, 0, 0);
        }
        public static void LeftMouseClick(int x, int y)
        {
            MoveTo(x, y);
            LeftClick();

        }

        public static void press1()
        {
            LeftMouseClick(28000, 25000);
            last2Moves("1");
        }
        public static void press2()
        {
            LeftMouseClick(33000, 25000);
            last2Moves("2");
        }
        //etc

        public static void rndPlay()
        {
            for (int r = 0; moveSucces != true && r < 10000; r++)
            {

                Random rndP = new Random();
                int rndPress = rndP.Next(1, 10);
                if (rndPress == 1)
                {
                    press1();
                }
                if (rndPress == 2)
                {
                    press2();
                }
                    //etc
             }
         }

当我运行rndPlay(); 时,我看到鼠标在移动,但似乎没有点击。是否有另一种方法可以让鼠标点击的位置更好/更快?

【问题讨论】:

  • 考虑WM_MOUSEMOVE,lParam 参数决定了鼠标的位置。低位字指定 x 坐标,高位字指定光标的 y 坐标。对 x 使用 16 位,对 y 使用 16 位意味着它无法处理大于 32767 的坐标。
  • 你的鼠标坐标好像很大。
  • @Phil1970 当指定MOUSEEVENTF_ABSOLUTE 时,鼠标相对位置以缇表示,在标准化的、设备无关的(65535,65535) 坐标系中。读取/写入函数时需要转换/标准化标准屏幕坐标,计算 TwipsPerPixelX/Y 值。人们可能想改用SendInputmouse_event 长期处于deprecated 状态。

标签: c# winforms dllimport


【解决方案1】:

除了对 X 和 Y 使用非常大的值之外,您的代码似乎还可以。 我用textbox 做了一个小例子来显示鼠标被点击的时间和地点,连接到一个简单的Forms 计时器来模拟点击。

public partial class Form1 : Form
{
   [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
   public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

   private const int MouseLeftDown = 0x02;
   private const int MouseLeftUp = 0x04;

   private readonly Timer _timer = new Timer();

   public Form1()
   {
      InitializeComponent();

      _timer.Interval = 1000;
      _timer.Tick += (s, a) => mouse_event(MouseLeftDown | MouseLeftUp, (uint)MousePosition.X, (uint)MousePosition.Y, 0, 0);
      _timer.Enabled = true;

      _textBox.Click += (s, a) => _textBox.AppendText($"Clicked at {PointToClient(MousePosition)}\n");
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多