0

所以实际上我在这里从“Vodemki”获得了一个非常快速和漂亮的代码 - 以 最快的方式获取像素颜色?

但唯一的问题是它扫描整个图像,而我只需要扫描我当前的鼠标位置

这是代码 -

HDC hdc, hdcTemp;
RECT rect;
BYTE* bitPointer;
int x, y;
int red, green, blue, alpha;

while(true)
{
hdc = GetDC(HWND_DESKTOP);
GetWindowRect(hWND_Desktop, &rect);
        int MAX_WIDTH = rect.right;
    int MAX_HEIGHT = rect.bottom;

hdcTemp = CreateCompatibleDC(hdc);
BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = MAX_WIDTH;
bitmap.bmiHeader.biHeight = MAX_HEIGHT;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = MAX_WIDTH * 4 * MAX_HEIGHT;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
SelectObject(hdcTemp, hBitmap2);
BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);

for (int i=0; i<(MAX_WIDTH * 4 * MAX_HEIGHT); i+=4)
{
    red = (int)bitPointer[i];
    green = (int)bitPointer[i+1];
    blue = (int)bitPointer[i+2];
    alpha = (int)bitPointer[i+3];

    x = i / (4 * MAX_HEIGHT);
    y = i / (4 * MAX_WIDTH);

    if (red == 255 && green == 0 && blue == 0)
    {
        SetCursorPos(x,y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        Sleep(50);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        Sleep(25);
    }
}
}

我不熟悉BitBlt,所以如果有人可以帮助我修改该代码,我将不胜感激

谢谢!

4

1 回答 1

1

好吧,我终于成功了。我的时间仍然有问题,让我的推杆很慢......我稍后会努力......

这是代码 -

//Globals

int sX, sY, x, y;

BYTE* sData = 0;

POINT cursorPos;

HDC hScreen;
HDC hdcMem;
HBITMAP hBitmap;
HGDIOBJ hOld;


void PixelFunction();   // Get the pixel rgb function

int main()
{
    PixelFunction();

    ReleaseDC(NULL, hScreen);
    DeleteDC(hdcMem);

    return 0;
}


void PixelFunction()
{
    int Red, Green, Blue;

    hScreen = GetDC(NULL);

    sX = GetDeviceCaps(hScreen, HORZRES);
    sY = GetDeviceCaps(hScreen, VERTRES);

    hdcMem = CreateCompatibleDC (hScreen);
    hBitmap = CreateCompatibleBitmap(hScreen, sX, sY);

    BITMAPINFOHEADER bm = {0};
    bm.biSize = sizeof(BITMAPINFOHEADER);
    bm.biPlanes = 1;
    bm.biBitCount = 32;
    bm.biWidth = sX;
    bm.biHeight = -sY;
    bm.biCompression = BI_RGB;
    bm.biSizeImage = 0; // 3 * sX * sY;

    while (1) {

        hOld = SelectObject(hdcMem, hBitmap);
        BitBlt(hdcMem, 0, 0, sX, sY, hScreen, 0, 0, SRCCOPY);
        SelectObject(hdcMem, hOld);

        free(sData);
        sData = (BYTE*)malloc(4 * sX * sY);

        GetDIBits(hdcMem, hBitmap, 0, sY, sData, (BITMAPINFO*)&bm, DIB_RGB_COLORS);

        GetCursorPos(&cursorPos);
        x = cursorPos.x;
        y = cursorPos.y;

        Red = sData[4 * ( (y * sX) + x) +2];
        Green = sData[4 * ( ( y * sX) + x) +1];
        Blue = sData[4 * ( (y * sX) + x)];

        // Check for color
        if (Red == 255 && Green == 0 && Blue == 0) {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            Sleep(5);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        Sleep(10);
    }
}

希望它有所帮助!

于 2013-06-30T15:07:39.340 回答