整个程序比较简单,实现方式如下:
1、首先创建对话框和对话框上的控件,然后就是注册热键,因为一旦鼠标自动点击开始,就只能通过键盘组合键来让整个过程停止。特别要注意的是,注册的热键尽量不要和其它程序注册的一样,否则会冲突,冲突后可能会失效,因此注册的越特殊越好。
这里注册了2组热键,CTRL+SHIFT+F4用来锁定或释放鼠标的位置,CTRL+SHIFT+F6用来停止鼠标自动点击的过程。注册的热键要在程序退出时主动调用UnregisterHotKey删除掉。
RegisterHotKey(hWndDlg, WM_MOUSE_POS, MOD_CONTROL | MOD_SHIFT, VK_F4);
RegisterHotKey(hWndDlg, WM_MOUSE_STOP, MOD_CONTROL | MOD_SHIFT, VK_F6);
2、这里使用CreateDialogParam创建非模式对话框,制定自己的消息循环,非模式对话框可以正常接收键盘、鼠标消息。
3、消息及逻辑处理
- 程序启动过后,首先创建定时器,用来实时获取鼠标的位置。
- 处理热键
3、启动按钮处理,启动定时器开始模拟鼠标点击,开始和结束时间的逻辑比较简单暂时没有实现。
通过mouse_event实现模拟鼠标点击,但杀软、防火墙、部分游戏则可能屏蔽mouse_event。这些程序一般会挂接钩子或采用驱动,识别消息到底是不是由外部真正的键盘和鼠标产生的,如果不是会丢弃掉,主要是为了防止记录键盘和鼠标的木马程序。
完整的程序代码如下:
#define WM_MOUSE_POS WM_USER + 100
#define WM_MOUSE_STOP WM_USER + 100
#define ID_TIMER_MOUSE 1
#define ID_TIMER_CLICK 2
BOOL CALLBACK WndDlg(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK AboutDlg(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hInst;
POINT g_mousePos = { 0, 0 };
bool g_lockMouse = false;
SYSTEMTIME g_startSt = { 0 }, g_endSt = { 0 };
int g_clickType = 0; //0表示单击,1表示双击
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hWndDlg = CreateDialogParam(hInst, "IDD_DIALOG1", NULL, (DLGPROC)WndDlg, NULL);
HWND hWndComboBox = GetDlgItem(hWndDlg, IDC_COMBO_SELECT_TYPE);
SendMessage(hWndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("单击左键"));
SendMessage(hWndComboBox, CB_ADDSTRING, 0, (LPARAM)TEXT("双击左键"));
SendMessage(hWndComboBox, CB_SETCURSEL, 0, 0);
SYSTEMTIME st = { 2020,1,0,1,1,0,0,0 }; //默认设置持续1小时,当心此处的时间的有效性
SendMessage(GetDlgItem(hWndDlg, IDC_DTP_CONTINUE), DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
SetWindowText(GetDlgItem(hWndDlg, IDC_INTERVAL), "1000");
SetWindowText(hWndDlg, "鼠标模拟");
ShowWindow(hWndDlg, SW_SHOW);
//注册的热键尽量不要和其它程序注册的一样,否则会冲突,冲突后会失效,杀软等也会屏蔽热键和mouse_event
RegisterHotKey(hWndDlg, WM_MOUSE_POS, MOD_CONTROL | MOD_SHIFT, VK_F4);
RegisterHotKey(hWndDlg, WM_MOUSE_STOP, MOD_CONTROL | MOD_SHIFT, VK_F6);
MSG msg;
while (GetMessage(&msg, NULL, NULL, NULL)) {
//if (msg.message == WM_KEYDOWN) {
// if (msg.wParam == VK_F3) g_lockMouse = !g_lockMouse;
//}
if (!IsDialogMessage(hWndDlg, &msg)) {// 如果消息没有被处理, 返回值为0
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterHotKey(hWndDlg, WM_MOUSE_POS);
UnregisterHotKey(hWndDlg, WM_MOUSE_STOP);
return 0;
}
void CALLBACK UpdateMousePosTimeProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
if (g_lockMouse) return;
GetCursorPos(&g_mousePos);
TCHAR str[64] = { 0 };
wsprintf(str, "%d , %d", g_mousePos);
SetWindowText(GetDlgItem(hwnd, IDC_MOUSE_POS), str);
}
void SetStartBtnState(HWND hwnd, BOOL state)
{
EnableWindow(GetDlgItem(hwnd, IDC_BTN_STOP), !state);
EnableWindow(GetDlgItem(hwnd, IDC_BTN_START), state);
}
//此处未处理开始和结束时间
void CALLBACK MouseClickTimeProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
SetCursorPos(g_mousePos.x, g_mousePos.y);
Sleep(200);
for (int i = 0; i <= g_clickType; i++) {
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, g_mousePos.x, g_mousePos.y, 0, 0);
}
}
void StartMouseClickTimer(HWND hwnd)
{
TCHAR str[1024] = { 0 };
GetWindowText(GetDlgItem(hwnd, IDC_INTERVAL), str, 1000);
int inteval = atoi(str);
if (inteval < 100) {
MessageBox(hwnd, "间隔时间要大于等于100", "错误", MB_YESNO);
return;
}
g_clickType = SendMessage(GetDlgItem(hwnd, IDC_COMBO_SELECT_TYPE), CB_GETCURSEL, 0, 0);
SetTimer(hwnd, ID_TIMER_CLICK, inteval, MouseClickTimeProc);
}
BOOL CALLBACK WndDlg(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_INITDIALOG:
SetTimer(hwnd, ID_TIMER_MOUSE, 100, UpdateMousePosTimeProc);
SetStartBtnState(hwnd, TRUE);
return TRUE;
case WM_HOTKEY:
if (HIWORD(lParam) == VK_F4 && LOWORD(lParam) == (MOD_CONTROL | MOD_SHIFT)) {
g_lockMouse = !g_lockMouse;
}
if (HIWORD(lParam) == VK_F6 && LOWORD(lParam) == (MOD_CONTROL | MOD_SHIFT)) {
SetStartBtnState(hwnd, TRUE);
KillTimer(hwnd, ID_TIMER_CLICK);
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_BTN_START:
SetStartBtnState(hwnd, FALSE);
StartMouseClickTimer(hwnd);
break;
case IDC_BTN_STOP:
SetStartBtnState(hwnd, TRUE);
KillTimer(hwnd, ID_TIMER_CLICK);
break;
}
return TRUE;
case WM_CLOSE:
KillTimer(hwnd, ID_TIMER_MOUSE);
KillTimer(hwnd, ID_TIMER_CLICK);
DestroyWindow(hwnd);
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
default:
return FALSE;
}
return FALSE;
}