#include <windows.h>
#include <iostream>
using namespace std;
//定义ID宏
#define ID_BEGIN 0x1
#define ID_END 0x2
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//压枪
void Control_Gun(int single, int sleep_time, int* move_single);
//压枪线程
void Gun_Thread();
//输出i线程
void Post_Thread();
int i = 0; //移动距离变量
int main() {
cout << "获取屏幕分辨率中....." << endl;
cout << "屏幕分辨率:" << GetSystemMetrics(0) << "x" << GetSystemMetrics(1) << endl;
cout << "注册热键中....." << endl;
RegisterHotKey(NULL, ID_BEGIN, NULL, VK_F1);
RegisterHotKey(NULL, ID_END, NULL, VK_F2);
cout << "热键注册完毕!" << endl;
MSG Msg;
//线程句柄
HANDLE gun_hThread = NULL;
HANDLE post_hThread = NULL;
while (GetMessage(&Msg, NULL, 0, 0)) {
if (Msg.message == WM_HOTKEY) {
switch (Msg.wParam)
{
case ID_BEGIN:
gun_hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Gun_Thread, NULL, NULL, NULL);
post_hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Post_Thread, NULL, NULL, NULL);
cout << "开始压枪....." << endl;
break;
case ID_END:
TerminateThread(gun_hThread, 0);
TerminateThread(post_hThread, 0);
cout << "暂停压枪....." << endl;
break;
}
}
}
system("pause"); //等待按键
return 0;
}
//压枪
void Control_Gun(int single, int sleep_time, int* move_single) {
POINT p, move_p; //鼠标当前位置
GetCursorPos(&p);
while (true) {
if (KEY_DOWN(VK_LBUTTON)) {
mouse_event(MOUSEEVENTF_MOVE, 0, single, NULL, GetMessageExtraInfo());
GetCursorPos(&move_p);
*move_single = move_p.y - p.y; //计算鼠标当前位置
Sleep(sleep_time);
}
}
}
//线程
void Gun_Thread() {
Control_Gun(2, 1, &i);
return;
}
//输出i线程
void Post_Thread() {
while (true) {
cout << i << endl;
Sleep(200);
}
}