【发布时间】:2011-07-20 16:58:36
【问题描述】:
我正在编写一个程序,它会在循环中为我点击几下鼠标。我创建了一个结构并将其设置为输入 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);
}
}
}
【问题讨论】:
-
是的,我们需要查看一些代码