【发布时间】:2020-08-13 16:56:01
【问题描述】:
#include <Windows.h>
#include <iostream>
#include <conio.h>
//void MouseRetracer(POINT mousePos, int noOfSaves, int delay)
//{
//
//}
int main(int argc, char* argv[])
{
std::cout << "Enter delay between 2 clicks... ";
int delay;
std::cin >> delay;
std::cout << "Hover the mouse to the desired location and press 'k'...\n";
//std::cin.get();
std::cout << "Press ENTER to start taking inputs!!!" << std::endl;
bool quit = true;
int noOfSaves = 0;
POINT mousePos[100];
while (quit)
{
if (_kbhit())
{
char kbKey = _getch();
if (kbKey == 'k')
{
GetCursorPos(&mousePos[noOfSaves]);
std::cout << noOfSaves + 1 << " positions saved...\n";
noOfSaves++;
}
if (kbKey == 'q')
quit = false;
}
}
std::cout << "DO NOT move mouse now!" << std::endl;
//MouseRetracer(mousePos, noOfSaves, delay);
for (int i = 0; i < noOfSaves; i++)
{
//std::cout << mousePos[i].x << '\t';
Sleep(delay);
mouse_event(MOUSEEVENTF_LEFTDOWN, mousePos[i].x, mousePos[i].y, NULL, NULL);
mouse_event(MOUSEEVENTF_LEFTUP, mousePos[i].x, mousePos[i].y, NULL, NULL);
std::cout << i + 1 << "th save clicked!\n";
}
/*std::cout << "Do this again? (y/n)\n";
char repeat;
std::cin >> repeat;
switch (repeat)
{
case 'y':
system("cls");
break;
case 'Y':
system("cls");
break;
default:
return 0;
}*/
return 0;
}
上面的代码可以正确地接受输入,即它创建POINT结构并正确保存数据mousePos[0]、mousePos[1]等(我通过Visual Studio 2019调试器检查)但它不能模拟使用mouse_event()的点击.没有错误或警告。问题似乎是mouse_event(),即使它在使用POINT mousePos; 的其他程序中运行良好。使用POINT mousePos[100] 似乎会带来麻烦。
提前谢谢你。
【问题讨论】:
-
请考虑使用可运行代码发布问题,以便人们更轻松地解决问题。否则,我们只是不断猜测您想要/需要什么。请检查how_to_ask
-
@dboy 你现在可以检查一下吗,我添加了完整的代码。
-
mouse_event: “此功能已被取代。请改用
SendInput。” -
但它适用于使用
POINT mousePos;的其他程序。定义POINT mousePos[100]似乎解决了问题。
标签: c++ c winapi visual-c++