Fork me on GitHub

C语言实现鼠标连点器

今天我们实现一个鼠标连点器,点击位置,点击间隔,点击次数,点击左键还是右键,双击还是单击都可以自定义

工具:VS2013

语言:C

有天看见同学在淘宝上抢购一款智能手表,原价169元才卖1.69元,可惜数量有限,没想到他们等了很久开始抢,一秒钟不到就被抢光了,我在旁边看着都能感受到他们的心碎。回到宿舍,我想:无非就是拼手速,为何我不做一个鼠标快速连点器呢?这样一秒钟点一百下,我就不信抢不到!
于是,我回去开始写起了代码:

头文件

click.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <tchar.h>
#include<stdio.h>
//#include<windows.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include <afxwin.h>
#define clt CLK_TCK
void Pos(int x,int y);
int prints(char*, int);
int getMapArray(char *mapname, unsigned char *maparray, int* mapwidth, int* mapheight);
unsigned int printimg(char* argv);
int move(int x, int y);
int click(int type, int double_click);
int clicks(int type, int double_click, int time, int count, int px, int py);
void printpos(int *x,int *y);

源文件

mouse.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define _CRT_SECURE_NO_WARNINGS
#include "click.h"
//const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标
//const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
//const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
//const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
//const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
//const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//模拟鼠标中键按下
//const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
//const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
/** mouse move
* x -- int, x-coordinate
* y -- int, y-coordinate
*/
//打印坐标
void printpos(int *x, int* y)
{
long zx = -1;
long zy = -1;
POINT ptB = { 0, 0 };
/*
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT, * PPOINT, NEAR * NPPOINT, FAR * LPPOINT;
*/
while (1)
{
LPPOINT xy = &ptB; //位置变量
GetCursorPos(xy); //获取鼠标当前位置
//如果鼠标移动,(即当前的坐标改变则打印出坐标)打印出做坐标。
if ((zx != xy->x) || (zy != xy->y))
{
Pos(0, 2);
printf("x=%4d,y=%4d\n", xy->x, xy->y);
zx = xy->x;
zy = xy->y;

}
if (GetAsyncKeyState(VK_ESCAPE))
{
*x = xy->x;
*y = xy->y;
break;
}
}
}
//设置打印位置
void Pos(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}

//鼠标移动
int move(int x, int y)
{
typedef BOOL(WINAPI *Fun1)(int x, int y);
//#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
//DECLARE_HANDLE(HINSTANCE);
HINSTANCE hDll;
hDll = LoadLibrary("user32.dll");
if (NULL == hDll)
{
fprintf(stderr, "load dll 'user32.dll' fail.");
return -1;
}
Fun1 SetCursorPos = (Fun1)GetProcAddress(hDll, "SetCursorPos");
if (NULL == SetCursorPos)
{
fprintf(stderr, "call function 'SetCursorPos' fail.");
FreeLibrary(hDll);
return -1;
}
SetCursorPos(x, y);
FreeLibrary(hDll);
return 0;
}

/** mouse click
* type -- int, 0:left click;1:right click
* double_click -- bool, true:double click; false: single click
*/
//鼠标点击
int clicks(int type, int double_click, int time, int count, int px, int py)
{
int t = 0;
while (1)
{
move(px, py);
Sleep(time);
click(type, double_click);
count--;
t++;
if (!count)break;
if (GetAsyncKeyState(VK_ESCAPE))
break;
}
return t;
}
int click(int type, int double_click)
{
int left_click = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
int right_click = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;
int clicktype;
HINSTANCE hDll;
typedef void(WINAPI*Fun2)
(
//typedef unsigned long DWORD;
DWORD dwFlags, // motion and click options
DWORD dx, // horizontal position or change
DWORD dy, // vertical position or change
DWORD dwData, // wheel movement
//typedef unsigned long _win64 *ULONG_PTR
ULONG_PTR dwExtraInfo // application-defined information
);

hDll = LoadLibrary("user32.dll");
if (NULL == hDll)
{
fprintf(stderr, "load dll 'user32.dll' fail.");
return -1;
}

Fun2 mouse_event = (Fun2)GetProcAddress(hDll, "mouse_event");
if (NULL == mouse_event)
{
fprintf(stderr, "call function 'mouse_event' fail.");
FreeLibrary(hDll);
return -1;
}
if (type == 0)
clicktype = left_click;
else
clicktype = right_click;
mouse_event(clicktype, 0, 0, 0, 0);
FreeLibrary(hDll);
if (double_click)
click(type, 0);
return 0;
}

laindian.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define _CRT_SECURE_NO_WARNINGS
#include "click.h"
int main()
{
int x, y, count;
int time;
int dou = 0;
int lef = 0;
int sum = 0;
char mess[250];
printf("欢迎使用鼠标连点器!\n按下ESC确认坐标:");
printpos(&x,&y);
printf("请输入延时(毫秒)和点击次数(0表示无限,随时可以按下ESC来中断点击):");
scanf("%d %d", &time, &count);
printf("十秒后开始点击!");
Sleep(10000);
sum = clicks(lef, dou, time, count, x, y);
wsprintf(mess, _T("%s\n%s%d%s"), _T("点击完毕"), _T("共点击:"), sum, _T("次"));
MessageBox(0, mess, _T("点击结束"), MB_OK);
return 0;
}

这样写完代码之后,记得把项目-属性-配置属性-常规-项目默认值-MFC的使用改为在共享 DLL 中使用 MFC
就完全搞定了!

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!