首页 > 解决方案 > 自动点击器 Visual Studio CS

问题描述

我试图制作自己的自动点击器。一切都很顺利。 如您所见,用户界面已启动

但是当我按下按钮时,它只会将状态从关闭更改为开启..但没有任何反应。

我尝试了很长时间来检查错误,但我没有找到任何错误。这是我的脚本,如果您发现任何错误,请告诉我!

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

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

        private const int LEFTUP = 0x0004;
        private const int LEFTDOWN = 0x0002;

        public form1()
        {
            InitializeComponent();
        }

        private void clicktimer_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int maxcps = (int)Math.Round(1000.0 / (trackBar1.Value + trackBar2.Value * 0.2));
            int mincps = (int)Math.Round(1000.0 / (trackBar1.Value + trackBar2.Value * 0.4));
            try
            {
                clicktimer.Interval = rnd.Next(mincps, maxcps);
            }
            catch
            {
                //Ignored
            }


            bool mousdown = MouseButtons == MouseButtons.Left;
            if (mousdown)
            {
                mouse_event(dwFlags:LEFTDOWN, dx:0,dy:0,cButtons:0,dwExtraInfo:0);
                Thread.Sleep(millisecondsTimeout: rnd.Next(1, 6));
                mouse_event(dwFlags: LEFTUP, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text.Contains("on"))
            {
                clicktimer.Start();
                button1.Text = "Toggle: off";
            }
            else
            {
                clicktimer.Stop(); 
                button1.Text = "Toggle: on";
            }
        }
    }
}

标签: c#

解决方案


我可以建议您使用以下内容更改您单击的代码部分:

int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(LEFTDOWN | LEFTUP, X, Y, 0, 0);

我认为您的问题是您开始单击但始终位于坐标 (0, 0) 处的屏幕位置,因此没有任何反应。
此外,您实际上并不需要 LEFTDOWN 动作和 LEFTUP 动作的超时

如果您不想单击刚刚设置光标的位置XY使用所需的值(坐标)


推荐阅读