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

标签 c++ windows-7

我正在编写一个程序,它会在循环中为我点击几下鼠标。我创建了一个结构并将其设置为键入 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/

相关文章:

c# - WIA: device.ExecuteCommand(CommandID.wiaCommandTakePicture);返回空

c++ - 如何使用 __builtin_ctz 加速二进制 GCD 算法?

c++ - 如何使用 Base 和 Derived 对象默认初始化 std::vector<Base> ?

c++ - 解决由于类之间的循环依赖而导致的构建错误

c++ - 如何在 Windows 7 上查看 .dmp 文件?

c# - 如何使用c#访问vista和7的缩略图缓存

c++ - 使用C++在链表中进行搜索操作

c++ - 在 C++ 中创建结构化(偶数)空间以显示数据

compiler-construction - 如何在 Eclipse 和 Windows 中指向我的编译器和链接器?

c# - DateTime.TryParse() 在 Windows 7 中失败