屏幕像素测试不符合预期(win32 c++)

发布于 2024-11-17 05:17:31 字数 2665 浏览 1 评论 0原文

我试图编写这段简短的草率代码来在屏幕上为我玩一个游戏,但它没有按我的预期响应。 在怀疑它可能与我的窗口有关后,我尝试让它盯着它mspaint,但它没有任何反应。

该程序需要 7 个点进行观察,然后按 Shift 键开始。然后它应该单击我在 RGB 内容上方的注释中所具有的颜色附近的任何像素。我尝试放宽这些范围,但我确实注意到它有一次点击了错误颜色的东西。 当我追踪它所获得的颜色值时,我注意到其中很多都是黑色的,这很奇怪,因为前窗中没有黑色像素。

(我正在使用Windows7,如果这意味着什么的话)

任何人都可以帮我找出问题所在吗?

#include <iostream>
#include <Windows.h>
using namespace std;

void ClickAt(int x,int y){
    SetCursorPos( x, y );
    Sleep(10);
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
    Sleep(10);
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
    Sleep(10);
}

int absDist(int a,int b)
{
    int diff = a - b;
    if( diff<0 ) diff*=-1;
    return diff;
}

LPPOINT mouse;
POINT testPoints[7];

int main()
{
    mouse = new POINT;
    BYTE state=FALSE;
    BYTE prevState=FALSE;

    cout<< "Hello, let\'s get ready to play the game.\n";


    cout<< "Please provide your click-points by clicking them now.\n";

    int clicksFound = 0;

    while(true){
        state = (bool)GetAsyncKeyState(VK_LBUTTON);
        if(state!=prevState){
            prevState=(bool)state;
            if((bool)state != 0){
                // a click happened
                GetCursorPos(mouse);
                testPoints[clicksFound].x = mouse->x;
                testPoints[clicksFound].y = mouse->y;
            }else{
                clicksFound++;
                if( clicksFound>=7 ) 
                    break;
            }
        }
    }
    cout<< "\aGreat! Now press shift when you\'re ready to start.\n";
    cout<< "(You\'ll press space to quit.)\n";

    while(true)
    {
        if( (bool)GetAsyncKeyState(VK_SHIFT) )
            break;
    }

    cout<< "\aNow playing the game.\n";

    COLORREF myPixel;
    int r,g,b;

    while(true)
    {
        HDC scrn = GetDC(NULL);

        // R 225
        // G 141
        // B 35
        for(int i=0;i<7;i++)
        {
            myPixel = GetPixel(scrn,testPoints[i].x,testPoints[i].y);
            if( myPixel==CLR_INVALID )
                cout<< "invalid\n";


            r = GetRValue(myPixel);
            g = GetGValue(myPixel);
            b = GetBValue(myPixel);

            if( absDist(r,225)<20 && absDist(g,141)<20 && absDist(b,35)<20 )
            {
                cout<< "click\n";
                ClickAt(testPoints[i].x,testPoints[i].y);
            }
        }

        Sleep(20);

        if( (bool)GetAsyncKeyState(VK_SPACE) )
            break;

        ReleaseDC(NULL,scrn);
    }

    cout<< "\aThank you for playing the game.\n";

    system("pause");

    return 0;
}

I tried to write this short sloppy code to play a game for me on my screen but it's not responding as I expected.
After suspecting that it might be something related to my window, I tried letting it stare it mspaint instead and it had no reaction.

The program takes in 7 points for observation, and you press shift to begin. Then it should click any pixels near the color I have in comments there above the rgb stuff. I've tried loosening those ranges but I did notice it click something of the wrong color once.
When I traced the color values it was getting, I noticed a lot of them were black, which is odd because there are no black pixels in the front window.

(I'm using windows7 if that means anything)

Can anyone help me figure out what's going wrong?

#include <iostream>
#include <Windows.h>
using namespace std;

void ClickAt(int x,int y){
    SetCursorPos( x, y );
    Sleep(10);
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
    Sleep(10);
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
    Sleep(10);
}

int absDist(int a,int b)
{
    int diff = a - b;
    if( diff<0 ) diff*=-1;
    return diff;
}

LPPOINT mouse;
POINT testPoints[7];

int main()
{
    mouse = new POINT;
    BYTE state=FALSE;
    BYTE prevState=FALSE;

    cout<< "Hello, let\'s get ready to play the game.\n";


    cout<< "Please provide your click-points by clicking them now.\n";

    int clicksFound = 0;

    while(true){
        state = (bool)GetAsyncKeyState(VK_LBUTTON);
        if(state!=prevState){
            prevState=(bool)state;
            if((bool)state != 0){
                // a click happened
                GetCursorPos(mouse);
                testPoints[clicksFound].x = mouse->x;
                testPoints[clicksFound].y = mouse->y;
            }else{
                clicksFound++;
                if( clicksFound>=7 ) 
                    break;
            }
        }
    }
    cout<< "\aGreat! Now press shift when you\'re ready to start.\n";
    cout<< "(You\'ll press space to quit.)\n";

    while(true)
    {
        if( (bool)GetAsyncKeyState(VK_SHIFT) )
            break;
    }

    cout<< "\aNow playing the game.\n";

    COLORREF myPixel;
    int r,g,b;

    while(true)
    {
        HDC scrn = GetDC(NULL);

        // R 225
        // G 141
        // B 35
        for(int i=0;i<7;i++)
        {
            myPixel = GetPixel(scrn,testPoints[i].x,testPoints[i].y);
            if( myPixel==CLR_INVALID )
                cout<< "invalid\n";


            r = GetRValue(myPixel);
            g = GetGValue(myPixel);
            b = GetBValue(myPixel);

            if( absDist(r,225)<20 && absDist(g,141)<20 && absDist(b,35)<20 )
            {
                cout<< "click\n";
                ClickAt(testPoints[i].x,testPoints[i].y);
            }
        }

        Sleep(20);

        if( (bool)GetAsyncKeyState(VK_SPACE) )
            break;

        ReleaseDC(NULL,scrn);
    }

    cout<< "\aThank you for playing the game.\n";

    system("pause");

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-11-24 05:17:31

我错了还是
ReleaseDC(NULL,scrn);
不要被释放,因为该行位于循环中的“break”之后。

Am I wrong or
ReleaseDC(NULL,scrn);
dont get released, because this line is after the "break" in the loop.

救赎№ 2024-11-24 05:17:31

如果其他程序不使用 GDI 绘制到屏幕(大多数游戏使用 Direct3D 或 OpenGL),则您无法以这种方式读取像素。

这个答案可能对您有帮助: 截取 DirectX 全屏应用程序的屏幕截图

If the other program isn't using GDI to draw to the screen (most games use Direct3D or OpenGL), then you can't read the pixels that way.

This answer might help you: Take screenshot of DirectX full-screen application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文