跳到正文

个人记录

连点器

Quirel约 22 分钟
原文
文章信息与版本

文章信息

发布
2026年10月17日
收录
2026年10月17日
更新
2026年11月11日
篇幅
约 22 分钟

版本

  1. v1当前
C++
// clicker_pro.cpp —— 连点器 Pro(Windows)
// 编译(MSVC):
//   cl /EHsc /O2 /std:c++17 clicker_pro.cpp user32.lib
// 编译(MinGW-w64,建议 posix 线程模型):
//   g++ -O2 -std=c++17 -static -o clicker_pro.exe clicker_pro.cpp -luser32

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <chrono>
#include <random>
#include <mutex>
#include <cstdio>
#include <cstdlib>

// ---------------- 配置 ----------------
struct Config {
	int button = 0;          // 0=左 1=右 2=中
	int clickMode = 0;       // 0=单击 1=双击 2=长按
	int intervalMin = 50;    // ms
	int intervalMax = 80;    // ms
	int holdMs = 200;        // 长按持续时间
	bool fixedPos = false;   // 是否定点
	int posX = 0, posY = 0;
	long long maxClicks = 0; // 0=无限
};

static Config g_cfg;
static std::mutex g_cfgMutex;
static std::string g_iniPath;
static HANDLE g_hConsole;

static std::atomic<bool> g_active{false};
static std::atomic<bool> g_quit{false};
static std::atomic<long long> g_count{0};

// ---------------- 工具 ----------------
static std::string GetIniPath()
{
	char buf[MAX_PATH]{};
	GetModuleFileNameA(nullptr, buf, MAX_PATH);
	std::string s(buf);
	auto pos = s.find_last_of("\\/");
	if (pos != std::string::npos) s = s.substr(0, pos + 1);
	return s + "clicker.ini";
}

static void ClearCurrentLine()
{
	CONSOLE_SCREEN_BUFFER_INFO info;
	if (GetConsoleScreenBufferInfo(g_hConsole, &info)) {
		printf("\r%*s\r", (int)info.dwSize.X, "");
	} else {
		printf("\r%80s\r", "");
	}
	fflush(stdout);
}

// ---------------- 配置持久化 ----------------
static void LoadConfig()
{
	g_iniPath = GetIniPath();
	const char* p = g_iniPath.c_str();
	g_cfg.button      = GetPrivateProfileIntA("main", "button", 0, p);
	g_cfg.clickMode   = GetPrivateProfileIntA("main", "clickMode", 0, p);
	g_cfg.intervalMin = GetPrivateProfileIntA("main", "intervalMin", 50, p);
	g_cfg.intervalMax = GetPrivateProfileIntA("main", "intervalMax", 80, p);
	g_cfg.holdMs      = GetPrivateProfileIntA("main", "holdMs", 200, p);
	g_cfg.fixedPos    = GetPrivateProfileIntA("main", "fixedPos", 0, p) != 0;
	g_cfg.posX        = GetPrivateProfileIntA("main", "posX", 0, p);
	g_cfg.posY        = GetPrivateProfileIntA("main", "posY", 0, p);
	
	char buf[64]{};
	GetPrivateProfileStringA("main", "maxClicks", "0", buf, sizeof(buf), p);
	g_cfg.maxClicks = std::strtoll(buf, nullptr, 10);
	if (g_cfg.maxClicks < 0) g_cfg.maxClicks = 0;
	if (g_cfg.intervalMin < 1) g_cfg.intervalMin = 1;
	if (g_cfg.intervalMax < g_cfg.intervalMin) g_cfg.intervalMax = g_cfg.intervalMin;
}

static void SaveConfig()
{
	const char* p = g_iniPath.c_str();
	auto W = [&](const char* key, const std::string& val) {
		WritePrivateProfileStringA("main", key, val.c_str(), p);
	};
	W("button",      std::to_string(g_cfg.button));
	W("clickMode",   std::to_string(g_cfg.clickMode));
	W("intervalMin", std::to_string(g_cfg.intervalMin));
	W("intervalMax", std::to_string(g_cfg.intervalMax));
	W("holdMs",      std::to_string(g_cfg.holdMs));
	W("fixedPos",    g_cfg.fixedPos ? "1" : "0");
	W("posX",        std::to_string(g_cfg.posX));
	W("posY",        std::to_string(g_cfg.posY));
	W("maxClicks",   std::to_string(g_cfg.maxClicks));
}

// ---------------- 鼠标输入 ----------------
static DWORD BtnDownFlag(int b) {
	switch (b) { case 1: return MOUSEEVENTF_RIGHTDOWN; case 2: return MOUSEEVENTF_MIDDLEDOWN; default: return MOUSEEVENTF_LEFTDOWN; }
}
static DWORD BtnUpFlag(int b) {
	switch (b) { case 1: return MOUSEEVENTF_RIGHTUP; case 2: return MOUSEEVENTF_MIDDLEUP; default: return MOUSEEVENTF_LEFTUP; }
}

static void SendBtn(int button, bool down)
{
	INPUT in{};
	in.type = INPUT_MOUSE;
	in.mi.dwFlags = down ? BtnDownFlag(button) : BtnUpFlag(button);
	SendInput(1, &in, sizeof(INPUT));
}

static void DoOneAction(const Config& cfg)
{
	if (cfg.fixedPos) SetCursorPos(cfg.posX, cfg.posY);
	
	if (cfg.clickMode == 2) {              // 长按
		SendBtn(cfg.button, true);
		std::this_thread::sleep_for(std::chrono::milliseconds(cfg.holdMs));
		SendBtn(cfg.button, false);
	} else if (cfg.clickMode == 1) {       // 双击
		SendBtn(cfg.button, true); SendBtn(cfg.button, false);
		std::this_thread::sleep_for(std::chrono::milliseconds(30));
		SendBtn(cfg.button, true); SendBtn(cfg.button, false);
	} else {                               // 单击
		SendBtn(cfg.button, true); SendBtn(cfg.button, false);
	}
}

// ---------------- 工作线程 ----------------
static void WorkerThread()
{
	std::random_device rd;
	std::mt19937 rng(rd() ^ static_cast<unsigned>(GetTickCount64()));
	
	while (!g_quit) {
		if (!g_active) {
			std::this_thread::sleep_for(std::chrono::milliseconds(5));
			continue;
		}
		
		Config cfg;
		{
			std::lock_guard<std::mutex> lk(g_cfgMutex);
			cfg = g_cfg;
		}
		
		if (cfg.maxClicks > 0 && g_count.load() >= cfg.maxClicks) {
			g_active = false;
			continue;
		}
		
		DoOneAction(cfg);
		g_count.fetch_add(1);
		
		int lo = cfg.intervalMin, hi = cfg.intervalMax;
		if (lo < 1) lo = 1;
		if (hi < lo) hi = lo;
		int wait = std::uniform_int_distribution<int>(lo, hi)(rng);
		
		auto next = std::chrono::steady_clock::now() + std::chrono::milliseconds(wait);
		while (std::chrono::steady_clock::now() < next) {
			if (g_quit) return;
			if (!g_active) break;
			auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
																			  next - std::chrono::steady_clock::now()).count();
			std::this_thread::sleep_for(std::chrono::milliseconds(left > 5 ? 5 : 1));
		}
	}
}

// ---------------- 热键 ----------------
static bool KeyJustPressed(int vk) {
	static bool prev[256] = {};
	if (vk < 0 || vk > 255) return false;
	bool down = (GetAsyncKeyState(vk) & 0x8000) != 0;
	bool pressed = down && !prev[vk];
	prev[vk] = down;
	return pressed;
}

// ---------------- 启动菜单 ----------------
static void PrintMenu()
{
	system("cls");
	const char* btn[] = {"左键", "右键", "中键"};
	printf("==================== 连点器 Pro ====================\n");
	printf("  1) 鼠标按键 : %s\n", btn[g_cfg.button]);
	if (g_cfg.clickMode == 2)
		printf("  2) 点击模式 : 长按 (%d ms)\n", g_cfg.holdMs);
	else
		printf("  2) 点击模式 : %s\n", g_cfg.clickMode == 1 ? "双击" : "单击");
	printf("  3) 间隔范围 : %d ~ %d ms (每次随机)\n", g_cfg.intervalMin, g_cfg.intervalMax);
	printf("  4) 定点点击 : %s", g_cfg.fixedPos ? "开启" : "关闭");
	if (g_cfg.fixedPos) printf("  (%d, %d)", g_cfg.posX, g_cfg.posY);
	printf("\n");
	if (g_cfg.maxClicks > 0) printf("  5) 次数限制 : %lld 次\n", g_cfg.maxClicks);
	else                     printf("  5) 次数限制 : 无限\n");
	printf("----------------------------------------------------\n");
	printf("  S) 开始连点\n");
	printf("  0) 退出\n");
	printf("====================================================\n");
	printf("请输入选项: ");
}

static void MenuSetButton()
{
	printf("按键 (0=左 1=右 2=中): ");
	int v = -1; std::cin >> v;
	if (v >= 0 && v <= 2) g_cfg.button = v;
	std::cin.clear(); std::cin.ignore(10000, '\n');
}

static void MenuSetMode()
{
	printf("模式 (0=单击 1=双击 2=长按): ");
	int v = -1; std::cin >> v;
	if (v >= 0 && v <= 2) g_cfg.clickMode = v;
	std::cin.clear(); std::cin.ignore(10000, '\n');
	if (g_cfg.clickMode == 2) {
		printf("长按持续时间 ms (10~10000): ");
		int t = 200; std::cin >> t;
		if (t >= 10 && t <= 10000) g_cfg.holdMs = t;
		std::cin.clear(); std::cin.ignore(10000, '\n');
	}
}

static void MenuSetInterval()
{
	printf("最小间隔 ms: ");
	int a = 0; std::cin >> a;
	printf("最大间隔 ms: ");
	int b = 0; std::cin >> b;
	if (std::cin && a >= 1 && b >= a && b <= 60000) {
		g_cfg.intervalMin = a;
		g_cfg.intervalMax = b;
	} else {
		printf("输入无效,需满足 1 <= min <= max <= 60000\n");
		Sleep(1500);
	}
	std::cin.clear(); std::cin.ignore(10000, '\n');
}

static void MenuSetFixedPos()
{
	system("cls");
	printf("定点点击设置\n");
	printf("  1) 3 秒后记录当前光标位置\n");
	printf("  2) 手动输入坐标\n");
	printf("  0) 关闭定点点击\n");
	printf("请选择: ");
	int v = -1; std::cin >> v;
	std::cin.clear(); std::cin.ignore(10000, '\n');
	
	if (v == 1) {
		printf("请把鼠标移到目标位置,3 秒后记录...\n");
		std::this_thread::sleep_for(std::chrono::seconds(3));
		POINT p; GetCursorPos(&p);
		g_cfg.posX = p.x; g_cfg.posY = p.y;
		g_cfg.fixedPos = true;
		printf("已记录 (%d, %d)\n", p.x, p.y);
		Sleep(1000);
	} else if (v == 2) {
		printf("X Y: ");
		int x = 0, y = 0; std::cin >> x >> y;
		if (std::cin) {
			g_cfg.posX = x; g_cfg.posY = y; g_cfg.fixedPos = true;
		}
		std::cin.clear(); std::cin.ignore(10000, '\n');
	} else if (v == 0) {
		g_cfg.fixedPos = false;
	}
}

static void MenuSetMaxClicks()
{
	printf("次数限制 (0=无限): ");
	long long v = -1; std::cin >> v;
	if (std::cin && v >= 0) g_cfg.maxClicks = v;
	std::cin.clear(); std::cin.ignore(10000, '\n');
}

static bool RunMenu()
{
	for (;;) {
		PrintMenu();
		std::string line;
		std::getline(std::cin, line);
		if (line.empty()) continue;
		char c = line[0];
		if (c == '0') return false;
		if (c == 's' || c == 'S') return true;
		switch (c) {
			case '1': MenuSetButton(); break;
			case '2': MenuSetMode(); break;
			case '3': MenuSetInterval(); break;
			case '4': MenuSetFixedPos(); break;
			case '5': MenuSetMaxClicks(); break;
			default: break;
		}
		SaveConfig();
	}
}

// ---------------- 主程序 ----------------
int main()
{
	SetProcessDPIAware();
	SetConsoleTitleA("连点器 Pro");
	g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	
	LoadConfig();
	if (!RunMenu()) { SaveConfig(); return 0; }
	SaveConfig();
	
	g_active = false;
	g_quit = false;
	g_count = 0;
	
	printf("\n热键说明:\n");
	printf("  F6  开始 / 暂停\n");
	printf("  F7  退出程序\n");
	printf("  F8  记录当前鼠标位置并开启定点\n");
	printf("  F9  切换定点开/关\n\n");
	printf("请切换到目标窗口,按 F6 开始。\n");
	
	std::thread worker(WorkerThread);
	
	auto lastPrint = std::chrono::steady_clock::now() - std::chrono::seconds(1);
	while (!g_quit) {
		if (KeyJustPressed(VK_F6)) {
			if (g_cfg.maxClicks > 0 && g_count.load() >= g_cfg.maxClicks) g_count = 0;
			g_active = !g_active;
			ClearCurrentLine();
			printf("[状态] %s\n", g_active ? "运行中" : "已暂停");
		}
		if (KeyJustPressed(VK_F7)) {
			g_quit = true;
		}
		if (KeyJustPressed(VK_F8)) {
			POINT p; GetCursorPos(&p);
			{
				std::lock_guard<std::mutex> lk(g_cfgMutex);
				g_cfg.posX = p.x; g_cfg.posY = p.y; g_cfg.fixedPos = true;
			}
			SaveConfig();
			ClearCurrentLine();
			printf("[定点] 已记录 (%d, %d)\n", p.x, p.y);
		}
		if (KeyJustPressed(VK_F9)) {
			bool on;
			{
				std::lock_guard<std::mutex> lk(g_cfgMutex);
				g_cfg.fixedPos = !g_cfg.fixedPos;
				on = g_cfg.fixedPos;
			}
			SaveConfig();
			ClearCurrentLine();
			printf("[定点] %s\n", on ? "开启" : "关闭");
		}
		
		auto now = std::chrono::steady_clock::now();
		if (now - lastPrint >= std::chrono::milliseconds(200)) {
			lastPrint = now;
			int m, lo, hi; bool fp;
			{
				std::lock_guard<std::mutex> lk(g_cfgMutex);
				m = g_cfg.clickMode; lo = g_cfg.intervalMin; hi = g_cfg.intervalMax; fp = g_cfg.fixedPos;
			}
			printf("\r[%s] 点击 %lld 次 | 间隔 %d~%dms | %s | %s    ",
				   g_active ? "开" : "停",
				   (long long)g_count.load(),
				   lo, hi,
				   m == 2 ? "长按" : (m == 1 ? "双击" : "单击"),
				   fp ? "定点" : "跟随光标");
			fflush(stdout);
		}
		
		std::this_thread::sleep_for(std::chrono::milliseconds(10));
	}
	
	g_active = false;
	worker.join();
	SaveConfig();
	
	printf("\n已退出,配置已保存到 %s\n", g_iniPath.c_str());
	return 0;
}
原文