gpt4 book ai didi

c++ - 自动鼠标点击使屏幕变黑

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:29 24 4
gpt4 key购买 nike

我正在编写一个程序,它会在循环中为我点击几下鼠标。我创建了一个结构并将其设置为键入 INPUT_MOUSE 以复制点击并使用 SendInput() 发送信息。一切都编译正确,可以称为“工作”程序,但我遇到了一个相当有趣的故障。我在我的笔记本电脑(windows vista)上写了这个程序,试了一下它运行良好。当我重写完全相同的代码并在我的桌面 (Windows 7) 上使用它时,当我运行该程序时,一旦我启动程序的自动化部分,我的屏幕就会变黑,就像它进入休眠模式时一样。该程序将在后台运行得很好,但自动化程序会黑掉我的屏幕,这有点痛苦。这是怎么回事?

我正在添加我的代码:

        #include "stdafx.h"

#include <windows.h>
#include <iostream>
#include <string>
#include <time.h>

using namespace std;





void clicky(int x, int y)
{
// 5 sec wait
clock_t run;
run = clock()+5*CLOCKS_PER_SEC;
while (clock() < run) {}


//plug in cursor coords and click down and up
SetCursorPos(x,y);

INPUT mouse;
mouse.type = INPUT_MOUSE;
mouse.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1,&mouse,sizeof(INPUT));

mouse.type = INPUT_MOUSE;
mouse.mi.dwFlags= MOUSEEVENTF_LEFTUP;
SendInput(1,&mouse,sizeof(INPUT));








}


void main()


{
int coords=0;
string h;
//find out how many clicks are needed
cout << "How many clicks will you need?";
cin >> coords;
//extra getline here without it when return is hit
//from entering the click amount it would also enter
//a cursor coordinate
getline(cin,h);


POINT p[21];
for (int count = 1;count<=coords;count++)
{
cout << "Place mouse cursor where you want a click and press return"<<endl;
//each time return is hit the cursor coordinates
//will be stored in the corresponding spot in
// the p array
string key = "r";
getline(cin,key);
GetCursorPos(&p[count]);
break;




}

string go;
cout << "Hit Return to initialize your click loop";
getline(cin,go);

while (true)
//infinite loop cycling through the users
//cursor coordinates and clicking
{
for(int click=1;click<=coords;click++)
{
int x = p[click].x;
int y = p[click].y;
clicky(x,y);
}

}

}

最佳答案

尝试在调用SendInput() 之前将INPUT 结构初始化为全零,例如

INPUT i;
ZeroMemory(&i, sizeof(i));

除此之外,请确保您指定的坐标不会太大。

当我犯了这两个错误中的任何一个时,我的屏幕就变成了空白(实际上,屏幕保护程序启动了)。

关于c++ - 自动鼠标点击使屏幕变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com