I wrote this and released it else where under a different name, but I never made use of it and neither did they so here you go :O Tool lazy to clean it up, although the code looks pretty neat already soh. Don't redistribute it in any form(be it compiled or not) without my permission. Under
Header :
[highlight=cpp]
#ifndef PIXELSCANNINGWRAPPER_H_
#define PIXELSCANNINGWRAPPER_H_
#include <windows.h>
struct Img
{
COLORREF crPixSearch[30][30];
int iX,iY;
int iWidth, iHeight;
};
struct Vector2D {
int iX,iY;
};
struct Rect {
int iX,iY,iWidth,iHeight;
};
Vector2D searchForImg(Img &Input, Rect &SearchBounds, float delay);
HBITMAP getImage(LPCWSTR path, bool resource);
int loadImage(Img &Input, HBITMAP &hBitmapSource,Rect rect, float delay);
#endif
[/highlight]
Source File :
[highlight=cpp]
#include <windows.h>
#include "pixelScanningWrapper.h"
Vector2D searchForImg(Img &Input, Rect &SearchBounds, float delay) {
HDC desktop = GetDC(HWND_DESKTOP);
int yIndex, xIndex, pixelsMatched, rowsMatched;
rowsMatched = pixelsMatched = yIndex = xIndex = 0;
Vector2D retLoc;
retLoc.iX = retLoc.iY = -1;
bool imageFound = false;
for(int y = SearchBounds.iY;; y++) {
if(rowsMatched == Input.iHeight) {
imageFound = true;
break;
}
if(y > SearchBounds.iY + SearchBounds.iWidth)
break;
for(int x = SearchBounds.iX;; x++) {
Sleep((DWORD)delay); //Typecast is to avoid warnings flagged by compiler
if((x + Input.iWidth) > SearchBounds.iX + SearchBounds.iWidth)
break;
COLORREF pixel = GetPixel(desktop, x, y); //FIX ON RELEASE
if(Input.crPixSearch[xIndex][yIndex] == pixel) {
xIndex++;
pixelsMatched++;
}
else{
xIndex = 0;
pixelsMatched = 0;
}
if(pixelsMatched == Input.iWidth) {
if(rowsMatched == 0) {
retLoc.iX = x - Input.iWidth;
retLoc.iY = y;
}else if(x - pixelsMatched != retLoc.iX) {
rowsMatched = 0;
break;
}
rowsMatched++;
xIndex = pixelsMatched = 0;
yIndex++;
break;
}
}
}
if(!imageFound)
retLoc.iX = retLoc.iY = -1;
return retLoc;
}
HBITMAP getImage(LPCWSTR path, bool resource) {
return (HBITMAP)LoadImage((HINSTANCE)GetModuleHandle(NULL ),
path,
IMAGE_BITMAP,
0,
0,
LR_DEFAULTSIZE | (resource ? 0 : LR_LOADFROMFILE));
}
int loadImage(Img &Input, HBITMAP &hBitmapSource,Rect rect, float delay)
{
HDC hImage = 0;
hImage = CreateCompatibleDC(0);
if(!hImage)
return -1;
SelectObject(hImage, hBitmapSource);
Input.iHeight = rect.iHeight;
Input.iWidth = rect.iWidth;
Input.iX = rect.iX;
Input.iY = rect.iY;
for(int Y = rect.iY;; Y++) {
if(Y >= rect.iHeight)
break;
for(int X = rect.iX;; X++) {
Sleep((DWORD)delay);
if(X >= rect.iWidth)
break;
Input.crPixSearch[X][Y] = GetPixel(hImage, X, Y);
}
}
return 1;
}[/highlight]
[b] I wrote this a while ago, I'm not doing it, but when you use this you should find problems(such as returning whole structs instead of pointers to them. So fix that.
Last edited by NextGen1; 02-07-2011 at 06:10 PM.