【问题标题】:automated mouse clicks make screen go blank自动鼠标点击使屏幕变为空白
【发布时间】: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);
                }

            }

        }

【问题讨论】:

  • 是的,我们需要查看一些代码

标签: c++ windows-7


【解决方案1】:

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

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

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

当我做错这两个时,屏幕变黑了(实际上,屏幕保护程序启动了)。

【讨论】:

  • ZeroMemory FTW ty Frerich Raabe 这真是一针见血
  • 你应该使用SecureZeroMemory
  • @Maxpm:真的吗?在回答stackoverflow.com/a/2013194/91757Hans Passant 中解释说:“使用 SecureZeroMemory 来初始化图标 [info] 结构是没有意义的。”我倾向于同意 - 在这种情况下,如果 ZeroMemory 调用被优化掉,以防 i 超出范围而不被读取,这有什么关系?
猜你喜欢
  • 2012-06-07
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多