c/c++找色
代码实现:
#include <iostream>
#include <atlimage.h>
using namespace std;
void FindColor(HWND hWnd, int color, int x, int y, int w, int h, int& a, int& b) {
HDC hDCScreen = ::GetDC(hWnd);
int nBitPerPixel = GetDeviceCaps(hDCScreen, BITSPIXEL);//获取到每个像素的bit数目
CImage m_MyImage;
m_MyImage.Create(w, h, nBitPerPixel);
//使用bitblt 将屏幕的DC画布上的内容 拷贝到CImage上
BitBlt(m_MyImage.GetDC(), x, y, w, h, hDCScreen, 0, 0, SRCCOPY);
int rgbAdd;
byte* pRealData = (byte*)m_MyImage.GetBits();
int pit = m_MyImage.GetPitch();
int bitCount = m_MyImage.GetBPP() / 8;
while (y < h) {
while (x < w) {
rgbAdd = (int)(*(pRealData + pit * y + x * bitCount + 2)) + (int)(*(pRealData + pit * y + x * bitCount + 1)) + (int)(*(pRealData + pit * y + x * bitCount));
if (rgbAdd == color) {
a = x;
b = y;
m_MyImage.ReleaseDC();
return;
}
x++;
}
y++;
}
a = -1;
b = -1;
m_MyImage.ReleaseDC();
}
int main() {
int x = 0, y = 0;
int rgb = 100 + 242 + 150;//代表r+g+b的总和
FindColor(NULL, rgb, 0, 0, 1920, 1080, x, y);
cout << "找到颜色点的坐标为:" << x << "," <<y << endl;
return 0;
}
参数详解:
第一个参数为:HWND:为窗口句柄,为NULL时代表全屏。
第二个参数:为要查找颜色的rgb总和。
第三到六个参数:为查找的范围。
最后两个参数:为找到颜色的坐标点。