#include <windows.h>
#include <iostream>
#pragma comment(lib, "Gdi32.lib ")
#pragma comment(lib, "user32.lib ")
using namespace std;
class Point
{
public:
int X;
int Y;
bool found;
};
int PixelSearch(Point* Pos, unsigned int oben_links_x,
unsigned int oben_links_y, unsigned int unten_rechts_x,
unsigned int unten_rechts_y, unsigned short rot_wert, unsigned short gruen_wert,
unsigned short blau_wert, unsigned short variation);
int sprintf(
char *buffer,
const char *format,
...
);
int main()
{
Point Pos;
HDC hdc=GetDC(0);
HPEN fPen=CreatePen(PS_SOLID, 20, RGB(52,120,150));
HGDIOBJ oOrig=SelectObject(hdc, fPen);
Ellipse(hdc, 100,100,30,30);
SelectObject(hdc, oOrig);
DeleteObject(fPen);
ReleaseDC(0, hdc);
PixelSearch(&Pos, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
52, 120, 150, 0);
if (Pos.found)
{
char test_buffer[51];
sprintf(test_buffer, "X: %i\r\nY: %i", Pos.X, Pos.Y);
MessageBox(0, test_buffer, "Testergebnis", 0);
}
system("Pause");
return 1;
}
int PixelSearch(Point* Pos, unsigned int sx, unsigned int sy, unsigned int dx, unsigned int dy, unsigned short rot,
unsigned short gruen, unsigned short blau, unsigned short variation=15)
{
int i;
BITMAPINFO info = {0};
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = dx;
info.bmiHeader.biHeight = dy;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 24;
HBITMAP bitmap;
RGBTRIPLE* memory;
HDC hdcDesktop = GetDC(0);
bitmap = CreateDIBSection(hdcDesktop, &info, DIB_RGB_COLORS, (void**)&memory, 0, 0);
if (!bitmap || !memory) return ReleaseDC(0, hdcDesktop);
HDC hdcMemory = CreateCompatibleDC(hdcDesktop);
HGDIOBJ oldbitmap = SelectObject(hdcMemory, bitmap);
BitBlt(hdcMemory, 0, 0, dx, dy, hdcDesktop, sx, sy, SRCCOPY);
SelectObject(hdcMemory, oldbitmap);
DeleteDC(hdcMemory);
int tbytes = dx * dy;
unsigned short RotDiff, GruenDiff, BlauDiff;
RGBTRIPLE *p = &memory[0];
Pos->found=false;
for (i=0; i<tbytes; i++)
{
RotDiff = p->rgbtRed-rot>-1 ? p->rgbtRed-rot : rot-p->rgbtRed;
GruenDiff = p->rgbtGreen-gruen>-1 ? p->rgbtGreen-gruen : gruen-p->rgbtGreen;
BlauDiff = p->rgbtBlue-blau>-1 ? p->rgbtBlue-blau : blau-p->rgbtBlue;
if (RotDiff<=variation && GruenDiff<=variation && BlauDiff<=variation)
{
sy = i / dx;
sx = i % dx;
sy = dy - sy - 1;
Pos->X=sx;
Pos->Y=sy;
Pos->found=true;
break;
}
++p;
}
DeleteObject(bitmap);
ReleaseDC(0, hdcDesktop);
}