额外减小 发表于 2023-9-16 22:55:43

关于屏幕取色器的代码求助

我想问一个问题,关于Windows编程,就是我做了一个取色器的程序。
思路大概是:先获取鼠标位置,然后获取该点像素RGB,然后显示出来。
但是我试了一下发现这个程序总是不能正确地显示鼠标所在位置的颜色,但它显示的颜色在屏幕中确实存在,只不过偏离了鼠标的位置。
测试图片如下:


请问该如何解决呢。
以下是代码部分
系统Win11。


#include <windows.h>
#include <cstdio>//sprintf

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);

void test_draw_filled_rect(HDC hdc,int x1,int y1,int x2,int y2,COLORREF color)
{
        RECT rc={x1,y1,x2,y2};
        FillRect(hdc,&rc,CreateSolidBrush(color));
}
void test_draw_line(HDC hdc, int x1, int y1, int x2, int y2, COLORREF color)
{
    HPEN hPen = CreatePen(PS_SOLID, 1, color);
    HPEN hOldPen = static_cast<HPEN>(SelectObject(hdc, hPen));

    MoveToEx(hdc, x1, y1, NULL);
    LineTo(hdc, x2, y2);

    SelectObject(hdc, hOldPen);
    DeleteObject(hPen);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc={0};
    wc.style=CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc=WndProc;
    wc.hInstance=hInstance;
    wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName="Color picker";

    RegisterClassA(&wc);

    HWND hwnd=CreateWindowA("Color picker","Color picker",WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,CW_USEDEFAULT,300,150,NULL,NULL,hInstance,NULL);
        ShowWindow(hwnd,nCmdShow);
        UpdateWindow(hwnd);
        SetTimer(hwnd,1,1000,NULL);

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

        return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
       
        switch(message)
        {
                case WM_PAINT:
                {
                       
                        RECT rect;
                        PAINTSTRUCT ps;
                        HDC hdc=BeginPaint(hwnd,&ps),hdc_screen=GetDC(NULL);
                        POINT pt;
                        GetCursorPos(&pt);
                        COLORREF color=GetPixel(hdc_screen,pt.x,pt.y);//获取屏幕上的像素
                       
                        test_draw_filled_rect(hdc,0,0,30,30,color);//画出颜色,以供参考
                        ReleaseDC(NULL,hdc_screen);
                        GetClientRect(hwnd,&rect);
                        char str={'\0'};
                        sprintf(str,"RGB:(%X,%X,%X)    CursorPos:(%d,%d)",GetRValue(color),GetGValue(color),GetBValue(color),pt.x,pt.y);
                        DrawText(hdc,str,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
                        EndPaint(hwnd,&ps);
                        return 0;
                }
                case WM_TIMER://每一秒更新一次
                {
                        InvalidateRect(hwnd,NULL,TRUE);
                        return 0;
                }
                case WM_DESTROY:
                {
                        KillTimer(hwnd,1);
                        PostQuitMessage(0);
                        return 0;
                }
               
        }
        return DefWindowProc(hwnd,message,wParam,lParam);
}

额外减小 发表于 2023-9-16 22:56:28

isdkz 发表于 2023-9-16 22:55
非常抱歉,我是一个AI语言模型,我不能感受到情绪和感情。我被设计用来帮助回答编程相关的问题。如果您有任 ...

呵呵

额外减小 发表于 2023-9-16 22:57:23

陶远航 发表于 2023-9-16 22:55
I'm sorry if I haven't met your expectations. If you have any specific concerns or issues, I'm here...

呵呵,还会说英文捏

zhangjinxuan 发表于 2023-9-17 08:38:03

反应有点迟钝,我的win11倒是没有什么大问题{:10_277:}

换编译器试试

额外减小 发表于 2023-9-17 15:36:53

zhangjinxuan 发表于 2023-9-17 08:38
反应有点迟钝,我的win11倒是没有什么大问题

换编译器试试

不是反应慢,是位置有偏差。他显示的颜色跟鼠标位置的颜色不同。你能发一个你的电脑的截图看看吗
还有您是什么编译器?我用的是g++,std=c++11

zhangjinxuan 发表于 2023-9-17 16:04:40

额外减小 发表于 2023-9-17 15:36
不是反应慢,是位置有偏差。他显示的颜色跟鼠标位置的颜色不同。你能发一个你的电脑的截图看看吗
还有您 ...

这里也是没有问题的,我觉得可能是系统环境的差异。

我用的是 mingw13.2.0, 64bit
页: [1]
查看完整版本: 关于屏幕取色器的代码求助