如何获取光标下的像素颜色?

发布于 2024-09-06 11:59:36 字数 214 浏览 4 评论 0原文

我需要一个快速的命令行应用程序来返回鼠标光标下像素的颜色。

我如何在 VC++ 中构建它,我需要类似于 这个,但理想情况下不是在 .NET 中,这样它可以每秒运行多次?

I need a fast command line app to return the color of the pixel under the mouse cursor.

How can I build this in VC++, I need something similar to this, but ideally not in .NET so it can be run many times per second?

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

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

发布评论

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

评论(1

染火枫林 2024-09-13 11:59:36

从我的头脑中,直接的方式:

#include <stdio.h>
#include <Windows.h>

int main(void) {
    POINT p;
    COLORREF color;
    HDC hDC;
    BOOL b;

    // Get the device context for the screen
    hDC = GetDC(NULL);
    if (hDC == NULL)
        return 3;

    // Get the current cursor position
    b = GetCursorPos(&p);
    if (!b)
        return 2;

    // Retrieve the color at that position
    color = GetPixel(hDC, p.x, p.y);
    if (color == CLR_INVALID)
        return 1;

    // Release the device context again
    ReleaseDC(GetDesktopWindow(), hDC);

    printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
    return 0;
}

预计到达时间:似乎有效,至少对我来说。

ETA2:添加了一些错误检查

ETA3:注释代码、编译的可执行文件和 Visual Studio 解决方案可以在 我的 SVN 存储库

Off the top of my head, the straightforward way:

#include <stdio.h>
#include <Windows.h>

int main(void) {
    POINT p;
    COLORREF color;
    HDC hDC;
    BOOL b;

    // Get the device context for the screen
    hDC = GetDC(NULL);
    if (hDC == NULL)
        return 3;

    // Get the current cursor position
    b = GetCursorPos(&p);
    if (!b)
        return 2;

    // Retrieve the color at that position
    color = GetPixel(hDC, p.x, p.y);
    if (color == CLR_INVALID)
        return 1;

    // Release the device context again
    ReleaseDC(GetDesktopWindow(), hDC);

    printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
    return 0;
}

ETA: Appears to work, at least for me.

ETA2: Added some error checking

ETA3: Commented code, compiled executable and a Visual Studio Solution can be found in my SVN repository.

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