C++获取鼠标坐标并移动鼠标

原文章地址:

C++获取鼠标坐标并移动鼠标

公司电脑强制设定10分钟无鼠标键盘输入便锁屏,锁屏之后要重新输入密码太麻烦。代码自动检测过去4.9分钟内鼠标是否发生了移动,如果没有发生移动则移动鼠标位置。


// testProject.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include<windows.h>
#include<time.h>
#include <string>
using namespace std;

int getNewValue(int last){
	while(true){
		int newValue = rand()%300 + 300;
		if(abs(newValue-last) > 50){
			return newValue;
		}
	}
}
 
int main(){
	cout<<"控制鼠标移动的程序已经开始运行^_^"<<endl;
	//HWND hwnd=GetForegroundWindow();
	//ShowWindow(hwnd,SW_HIDE); //将cmd窗口隐藏起来,让程序在后台运行
    POINT p;
	POINT last_p;
    while(true){
		GetCursorPos(&last_p);//获取鼠标坐标

		Sleep(1000 * 60 * 4.9);

        GetCursorPos(&p);//获取鼠标坐标
		//日志记录
		time_t timePointer;
		time(&timePointer);
		char temp[64];
		strftime(temp,sizeof(temp),"%Y-%m-%d %H:%M:%S",localtime(&timePointer));
		cout<<(string)temp<<" ";
		if(p.x == last_p.x && p.y == last_p.y){
			SetCursorPos(getNewValue(p.x),getNewValue(p.y));//更改鼠标坐标

			POINT tempPoint;
			GetCursorPos(&tempPoint);
			printf("no move,change position to:%d,%d\n",tempPoint.x,tempPoint.y);
		}else{
			printf("moved,do nothing.\n");
		}
    } 
    return 0;
}


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值