c/c++找色功能实现

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总和。

第三到六个参数:为查找的范围。

最后两个参数:为找到颜色的坐标点。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值