參考自:https://blog.csdn.net/bjbz_cxy/article/details/80762469
#include<wingdi.h> //與下一行一起,是GetPixel的頭文件
#pragma comment(lib, "gdi32.lib")
void GetColor()
{
POINT Mouse_x_y;
if (FALSE == GetCursorPos(&Mouse_x_y)) //獲取鼠標座標
{
printf("error:無法獲取鼠標指針位於屏幕的座標值");
}
HDC hdc = GetDC(NULL);
COLORREF Windows_Hdc_RGB = GetPixel(hdc, Mouse_x_y.x, Mouse_x_y.y);
int r = GetRValue(Windows_Hdc_RGB);
int g = GetGValue(Windows_Hdc_RGB);
int b = GetBValue(Windows_Hdc_RGB);
qDebug() << Mouse_x_y.x << Mouse_x_y.y << r << g << b;
}
與定時器或者MouseMoveEvent事件結合,可根據需求獲取鼠標位置的RGB值。
#pragma comment(lib, "gdi32.lib")表示引用gdi32這個庫,在msvc編譯器可正常運行,但是mingw編譯器無法引用,改爲在pro文件中增加
LIBS += -lgdi32
可正常運行。