找不到我的代码有什么问题:(

时间:2010-04-18 23:17:25

标签: c++ winapi

我的代码点是我按f1,它将扫描500像素和500像素,并将它们放在一个数组(它只需要一个500×500的屏幕框)。然后,当我点击结束时,它将仅点击颜色黑色或...我设置它。 无论如何它一直在做奇怪的东西,我找不到原因:

#include <iostream>
#include <windows.h>

using namespace std;

COLORREF rgb[499][499];
HDC hDC = GetDC(HWND_DESKTOP);

POINT main_coner;

BYTE rVal;
BYTE gVal;
BYTE bVal;

int red;  
int green;
int blue; 

int ff = 0;

int main()
{
for(;;)
{
    if(GetAsyncKeyState(VK_F1))
    {
        cout << "started";
        int a1 = 0;
        int a2 = 0;

        GetCursorPos(&main_coner);

        int x = main_coner.x;
        int y = main_coner.y;

        for(;;)
        {
            //cout << a1 << "___" << a2 << "\n";
            rgb[a1][a2] = GetPixel(hDC, x, y);
            a1++;
            x++;

            if(x > main_coner.x + 499)
            {
                y++;
                x = main_coner.x;
                a1 = 0;
                a2++;

            }
            if(y > main_coner.y + 499)
            {
                ff = 1;
                break;
            }
        }

        cout << "done";
        break;
    }
    if(ff == 1)
      break;

}

for(;;)
{
        if(GetAsyncKeyState(VK_END))
        {
            GetCursorPos(&main_coner);

            int x = main_coner.x;
            int y = main_coner.y;

            int a1 = -1;
            int a2 = -1;

            for(;;)
            {
                x++;
                a1++;
                rVal = GetRValue(rgb[a1][a2]);
                gVal = GetGValue(rgb[a1][a2]);
                bVal = GetBValue(rgb[a1][a2]);

                red   = (int)rVal;   // get the colors into __int8
                green = (int)gVal;   // get the colors into __int8
                blue  = (int)bVal;   // get the colors into __int8

                if(red == 0 && green == 0 && blue == 0)
                {

                    SetCursorPos(main_coner.x + x, main_coner.y + y);

                    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    Sleep(10);
                    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    Sleep(100);

                }

                if(x > main_coner.x + 499)
                {
                    a1 = 0;
                    a2++;
                }

                if(y > main_coner.y + 499)
                {
                    Sleep(100000000000);

                    break;
                }
                if(GetAsyncKeyState(VK_CONTROL))
                {
                    Sleep(100000);
                    break;
                }

            }

        }
}

for(;;)
{


    if(GetAsyncKeyState(VK_END))
    {
        break;
    }

}

return 0;
}

任何人都能看到我的代码出了什么问题:( (随意添加标签)

1 个答案:

答案 0 :(得分:2)

如果您希望rgb数组包含500x500个条目(编号为[0] [0]至[499] [499]),则需要将其声明为COLORREF rgb[500][500];

此外,请确保您不要尝试访问rgb[a1][a2]

所在的a2 == -1