Untitled

 avatar
unknown
plain_text
2 months ago
3.6 kB
4
Indexable
#include <windows.h>
#include <iostream>

HHOOK keyboardHook;

void CaptureColor() {
    SetProcessDPIAware();

    HWND hwnd = FindWindowA(NULL, "Windowed Projector (Source) - Tibia");
    if (!hwnd) {
        std::cerr << "Window not found!" << std::endl;
        return;
    }

    HDC hdcScreen = GetDC(NULL);
    HDC hdcWindow = GetDC(hwnd);
    if (!hdcScreen || !hdcWindow) {
        std::cerr << "Failed to get device context!" << std::endl;
        return;
    }

    POINT p;
    GetCursorPos(&p);
    std::cout << "Mouse Screen Coordinates: X=" << p.x << " Y=" << p.y << std::endl;

    COLORREF screenColor = GetPixel(hdcScreen, p.x, p.y);
    std::cout << "Screen Color: R=" << (int)GetRValue(screenColor)
              << " G=" << (int)GetGValue(screenColor)
              << " B=" << (int)GetBValue(screenColor) << std::endl;

    RECT windowRect, clientRect;
    GetWindowRect(hwnd, &windowRect);
    GetClientRect(hwnd, &clientRect);

    int borderX = ((windowRect.right - windowRect.left) - clientRect.right) / 2;
    int titleBarHeight = (windowRect.bottom - windowRect.top) - clientRect.bottom - borderX;

    std::cout << "Window Rect: Left=" << windowRect.left << " Top=" << windowRect.top
              << " Right=" << windowRect.right << " Bottom=" << windowRect.bottom << std::endl;

    std::cout << "Client Rect: Width=" << clientRect.right << " Height=" << clientRect.bottom << std::endl;
    std::cout << "Title Bar Height: " << titleBarHeight << " Border Width: " << borderX << std::endl;

    POINT clientP = { p.x - windowRect.left - borderX, p.y - windowRect.top - titleBarHeight };
    std::cout << "Converted Client Coordinates: X=" << clientP.x << " Y=" << clientP.y << std::endl;

    // Create a compatible DC and a new bitmap
    HDC memDC = CreateCompatibleDC(hdcWindow);
    HBITMAP hBitmap = CreateCompatibleBitmap(hdcWindow, clientRect.right, clientRect.bottom);
    SelectObject(memDC, hBitmap);

    // Copy the window's content to the memory DC
    BitBlt(memDC, 0, 0, clientRect.right, clientRect.bottom, hdcWindow, 0, 0, SRCCOPY);

    // Get the color from the captured bitmap
    COLORREF clientColor = GetPixel(memDC, clientP.x, clientP.y);
    std::cout << "Client Color: R=" << (int)GetRValue(clientColor)
              << " G=" << (int)GetGValue(clientColor)
              << " B=" << (int)GetBValue(clientColor) << std::endl;

    // Cleanup
    DeleteObject(hBitmap);
    DeleteDC(memDC);
    ReleaseDC(NULL, hdcScreen);
    ReleaseDC(hwnd, hdcWindow);
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        KBDLLHOOKSTRUCT* pKey = (KBDLLHOOKSTRUCT*)lParam;
        if (wParam == WM_KEYDOWN && pKey->vkCode == VK_INSERT) {
            CaptureColor();
        }
        if (wParam == WM_KEYDOWN && pKey->vkCode == VK_ESCAPE) {
            std::cout << "Exiting...\n";
            PostQuitMessage(0);
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main() {
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
    if (!keyboardHook) {
        std::cerr << "Failed to install keyboard hook!" << std::endl;
        return 1;
    }

    std::cout << "Press INSERT to capture color, ESC to exit.\n";

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnhookWindowsHookEx(keyboardHook);
    return 0;
}
Editor is loading...
Leave a Comment