VC模拟鼠标动作

// mouse.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <vector>

enum ENUM_ACTION
{
	ACTION_MOVE =0,
	ACTION_CLICK,
	ACTION_SLEEP
};

typedef struct _ACTION_STRUCT
{
	ENUM_ACTION action;
	int x;
	int y;
	int sleep;

}ACTION_STRUCT,*PACTION_STRUCT;

void DoMove(int x, int y)
{
	SetCursorPos(x, y);
}

void DoClick(int x, int y)
{
	SetCursorPos(x, y);
	Sleep(10);
	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	Sleep(10);
	mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

void DoSleep(int x)
{
	Sleep(x);
}

int main(int arc,char* argv[])
{
	if (arc > 1)
	{
		while (1)
		{
			POINT pt;
			GetCursorPos(&pt);
			printf("%d,%d\n", pt.x, pt.y);
			Sleep(100);

		}
	}

	std::vector<ACTION_STRUCT> arr;
	char buff[512] = { 0 };
	FILE* fp = fopen("script.txt", "rt");

	int loop = 1;
	while (fgets(buff, sizeof(buff), fp))
	{
		char* p = strstr(buff, "move ");
		if (p != NULL)
		{
			p = p + strlen("move ");
			int x = atoi(p);
			p = strstr(p, ",");
			int y = atoi(p + 1);
			printf("move %d %d\n", x, y);

			ACTION_STRUCT a;
			a.action = ACTION_MOVE;
			a.x = x;
			a.y = y;
			arr.push_back(a);


			continue;
		}

		p = strstr(buff, "sleep ");
		if (p != NULL)
		{
			p = p + strlen("sleep ");
			int s = atoi(p);
			printf("sleep %d\n", s);

			ACTION_STRUCT a;
			a.action =ACTION_SLEEP;
			a.sleep = s;
			arr.push_back(a);

			continue;
		}

		p = strstr(buff, "click ");
		if (p != NULL)
		{
			p = p + strlen("click ");
			int x = atoi(p);
			p = strstr(p, ",");
			int y = atoi(p + 1);
			printf("click %d %d\n", x, y);

			ACTION_STRUCT a;
			a.action = ACTION_CLICK;
			a.x = x;
			a.y = y;
			arr.push_back(a);

			continue;
		}

		p = strstr(buff, "loop ");
		if (p != NULL)
		{
			p = p + strlen("loop ");
			loop = atoi(p);
			continue;
		}


	}
	fclose(fp);


	printf("loop=%d\n", loop);

	int i = 0;
	while(1)
	{
		for (int k = 0; k < arr.size(); k++)
		{
			if (arr[k].action == ACTION_MOVE)
			{
				DoMove(arr[k].x, arr[k].y);
			}
			else if (arr[k].action == ACTION_CLICK)
			{
				DoClick(arr[k].x, arr[k].y);
			}
			else if (arr[k].action == ACTION_SLEEP)
			{
				DoSleep(arr[k].sleep);
			}
		}

		if (loop == 0)
		{
			continue;
		}
		else if(i++>loop)
		{
			break;
		}

		Sleep(5000);

	}
	
	printf("exit\n");

	//Sleep(1000);
	//POINT lpPoint;

	//for (int i = 0; i < 10; i++)
	//{
	//	lpPoint.x = 100+i*100;
	//	lpPoint.y = 100;
	//	SetCursorPos(lpPoint.x, lpPoint.y);
	//	printf("%d\n", i);
	//	Sleep(500);
	//}

	//mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	//mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);



}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

站长漫谈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值