Code:
#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#define BMPX 12
#define BMPY 9
#define BMPWIDTH 8
#define BMPHEIGHT 8
#define BOARDX (38 - BMPX)
#define BOARDY (78 - BMPY)
#define CELLWIDTH 18
#define CELLHEIGHT 18
#define CELLCOUNTX 9
#define CELLCOUNTY 9
#define HIGHNUMBER 4
#define SCREENWIDTH 1680
#define SCREENHEIGHT 1050
#define BLANK 0
#define FLAG (HIGHNUMBER + 1)
#define EPSILON 30
#define RED24BIT(c) (int)(((c) >> 0) & 255)
#define GREEN24BIT(c) (int)(((c) >> 8) & 255)
#define BLUE24BIT(c) (int)(((c) >> 16) & 255)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
struct INFO
{
COLORREF Color;
int x;
int y;
};
int BoardInfo[CELLCOUNTX][CELLCOUNTY];
static HWND MainWindow;
static HINSTANCE MainInstance;
static char ClassName[] = "Minesweeper Helper";
INFO ColorInfo[HIGHNUMBER + 2];
int WindowX = -1, WindowY = -1;
bool InitWindow(HINSTANCE Instance, int ShowCmd);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Message,
WPARAM wParam, LPARAM lParam);
bool GetMinesweeperLocation();
BOOL Helper_OnCreate(HWND Window, CREATESTRUCT FAR* CreateStruct);
void Helper_OnPaint(HWND Window);
void Helper_OnDestroy(HWND Window);
void UpdateBoard(void);
void FindClicks(void);
int _stdcall WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CmdLine , int ShowCmd)
{
MSG Msg;
if (!InitWindow(Instance, ShowCmd))
{
MessageBox(0, "Could not create the window!\nPeace Out!", "Huge Problem", MB_OK);
return 0;
}
if (!GetMinesweeperLocation())
{
MessageBox(MainWindow, "Could not find minesweeper window!\nPeaceOut", "Big Problem", MB_OK);
return 0;
}
while (true)
{
if (PeekMessage(&Msg, 0, NULL, NULL, PM_REMOVE))
{
if (Msg.message == WM_QUIT)
break;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UpdateBoard();
FindClicks();
Sleep(30);
}
return Msg.wParam;
}
bool InitWindow(HINSTANCE Instance, int ShowCmd)
{
WNDCLASSEX Class;
Class.cbClsExtra = 0;
Class.cbSize = sizeof(Class);
Class.cbWndExtra = 0;
Class.hbrBackground = GetStockBrush(WHITE_BRUSH);
Class.hCursor = LoadCursor(NULL, IDC_ARROW);
Class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
Class.hInstance = Instance;
Class.lpfnWndProc = WindowProc;
Class.lpszClassName = ClassName;
Class.lpszMenuName = NULL;
Class.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&Class))
return false;
MainInstance = Instance;
MainWindow = CreateWindowEx(NULL, ClassName, ClassName,WS_OVERLAPPEDWINDOW,
0, 0, 300, 300, NULL, NULL, Instance, NULL);
if (MainWindow == NULL)
return false;
ShowWindow(MainWindow, ShowCmd);
UpdateWindow(MainWindow);
return true;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Message,
WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
HANDLE_MSG(hWnd, WM_CREATE, Helper_OnCreate);
HANDLE_MSG(hWnd, WM_PAINT, Helper_OnPaint);
// HANDLE_MSG(hWnd, WM_KEYUP, Edge_OnKey);
HANDLE_MSG(hWnd, WM_DESTROY, Helper_OnDestroy);
default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
}
BOOL Helper_OnCreate(HWND Window, CREATESTRUCT FAR* CreateStruct)
{
ColorInfo[0].Color = RGB(196, 207, 229);
ColorInfo[0].x = 9;
ColorInfo[0].y = 8;
ColorInfo[1].Color = RGB(64, 80, 191);
ColorInfo[1].x = 9;
ColorInfo[1].y = 9;
ColorInfo[2].Color = RGB(31, 104, 5);
ColorInfo[2].x = 8;
ColorInfo[2].y = 3;
ColorInfo[3].Color = RGB(166, 5, 5);
ColorInfo[3].x = 8;
ColorInfo[3].y = 8;
ColorInfo[4].Color = RGB(2, 1, 133);
ColorInfo[4].x = 12;
ColorInfo[4].y = 10;
ColorInfo[FLAG].Color = RGB(252, 3, 9);
ColorInfo[FLAG].x = 8;
ColorInfo[FLAG].y = 7;
return true;
}
bool GetMinesweeperLocation()
{
HDC MainDC = GetDC(0);
HDC WindowDC = GetDC(MainWindow);
int x1, y1, x2, y2;
unsigned long BmpPixels[BMPWIDTH][BMPHEIGHT];
unsigned long *ScrnPixels = new unsigned long[SCREENWIDTH*SCREENHEIGHT];
char Message[100];
HBITMAP CornerBmp = LoadBitmap(MainInstance, "Corner");
HBITMAP ScreenBmp = CreateCompatibleBitmap(MainDC, SCREENWIDTH, SCREENHEIGHT);
if (!CornerBmp)
{
MessageBox(MainWindow, "Corner Bitmap is MIA", "Chillin'", MB_OK);
return false;
}
HDC BmpDC = CreateCompatibleDC(MainDC);
HBITMAP OldBmp = SelectBitmap(BmpDC, CornerBmp);
bool FoundWindow = false;
for (y1 = 0; y1 < BMPHEIGHT; y1++)
{
for (x1 = 0; x1 < BMPWIDTH; x1++)
{
BmpPixels[x1][y1] = GetPixel(BmpDC, x1, y1);
}
}
SelectBitmap(BmpDC, ScreenBmp);
BitBlt(BmpDC, 0, 0, SCREENWIDTH, SCREENHEIGHT, MainDC, 0, 0, SRCCOPY);
for (y1 = 0; y1 < SCREENHEIGHT; y1++)
{
for (x1 = 0; x1 < SCREENWIDTH; x1++)
{
ScrnPixels[x1 + y1 * SCREENWIDTH] = GetPixel(BmpDC, x1, y1);
}
}
SelectBitmap(BmpDC, OldBmp);
for (y1 = 0; (y1 < SCREENHEIGHT - BMPHEIGHT) && (!FoundWindow); y1++)
{
for (x1 = 0; (x1 < SCREENWIDTH - BMPWIDTH) && (!FoundWindow); x1++)
{
FoundWindow = true;
for (y2 = 0; (y2 < BMPHEIGHT) && FoundWindow; y2++)
{
for (x2 = 0; (x2 < BMPWIDTH) && FoundWindow; x2++)
{
if (BmpPixels[x2][y2] != ScrnPixels[x1+x2+(y1+y2) * SCREENWIDTH])
FoundWindow = false;
}
}
if (FoundWindow)
{
WindowX = x1;
WindowY = y1;
}
}
}
if (!FoundWindow)
return false;
sprintf(Message, "Window location: (%d, %d)", WindowX - BMPX, WindowY - BMPY);
TextOut(WindowDC, 10, 10, Message, strlen(Message));
for (y1 = 0; y1 < CELLCOUNTY; y1++)
{
for (x1 = 0; x1 < CELLCOUNTX; x1++)
{
BoardInfo[x1][y1] = -1;
}
}
DeleteDC(BmpDC);
ReleaseDC(0, MainDC);
ReleaseDC(MainWindow, WindowDC);
return true;
}
void Helper_OnPaint(HWND Window)
{
PAINTSTRUCT PaintStruct;
char Message[100];
HDC PaintDC = BeginPaint(Window, &PaintStruct);
if (WindowX != -1 && WindowY != -1)
{
sprintf(Message, "Window location: (%d, %d)", WindowX - BMPX, WindowY - BMPY);
TextOut(PaintDC, 10, 10, Message, strlen(Message));
}
EndPaint(Window, &PaintStruct);
}
void Helper_OnDestroy(HWND Window)
{
PostQuitMessage(0);
}
void UpdateBoard(void)
{
int x, y, i;
HDC MainDC = GetDC(0);
HDC WindowDC = GetDC(MainWindow);
char Message[100];
HBITMAP ScreenBmp = CreateCompatibleBitmap(MainDC, CELLWIDTH * CELLCOUNTX, CELLHEIGHT * CELLCOUNTY);
HDC ScreenDC = CreateCompatibleDC(MainDC);
HBITMAP OldBmp = SelectBitmap(ScreenDC, ScreenBmp);
BitBlt(ScreenDC, 0, 0, 80, 80,
MainDC, WindowX + BOARDX, WindowY + BOARDY, SRCCOPY);
BitBlt(ScreenDC, 80, 0, 82, 80,
MainDC, WindowX + BOARDX + 81, WindowY + BOARDY, SRCCOPY);
BitBlt(ScreenDC, 0, 80, 80, 82,
MainDC, WindowX + BOARDX, WindowY + BOARDY + 81, SRCCOPY);
BitBlt(ScreenDC, 80, 80, 82, 82,
MainDC, WindowX + BOARDX + 81, WindowY + BOARDY + 81, SRCCOPY);
for (y = 0; y < CELLCOUNTY; y++)
{
for (x = 0; x < CELLCOUNTX; x++)
{
BoardInfo[x][y] = -1;
// if (BoardInfo[x][y] == -1)
// {
// BOARDX/y may be wrong
// if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[BLANK].x,
// y * CELLHEIGHT + ColorInfo[BLANK].y) == ColorInfo[BLANK].Color)
// {
// BoardInfo[x][y] = BLANK;
COLORREF Color = GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[0].x,
y * CELLHEIGHT + ColorInfo[0].y);
if (abs(RED24BIT(Color) - RED24BIT(ColorInfo[0].Color)) < EPSILON)
{
if (abs(GREEN24BIT(Color) - GREEN24BIT(ColorInfo[0].Color)) < EPSILON)
{
if (abs(BLUE24BIT(Color) - BLUE24BIT(ColorInfo[0].Color)) < EPSILON)
{
BoardInfo[x][y] = 0;
}
}
}
for (i = 0; i <= HIGHNUMBER; i++)
{
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x,
y * CELLHEIGHT + ColorInfo[i].y) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x - 1,
y * CELLHEIGHT + ColorInfo[i].y) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x + 1,
y * CELLHEIGHT + ColorInfo[i].y) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x,
y * CELLHEIGHT + ColorInfo[i].y - 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x,
y * CELLHEIGHT + ColorInfo[i].y + 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x - 1,
y * CELLHEIGHT + ColorInfo[i].y + 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x + 1,
y * CELLHEIGHT + ColorInfo[i].y + 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x - 1,
y * CELLHEIGHT + ColorInfo[i].y - 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[i].x + 1,
y * CELLHEIGHT + ColorInfo[i].y - 1) == ColorInfo[i].Color)
{
BoardInfo[x][y] = i;
continue;
}
}
// }
if (GetPixel(ScreenDC, x * CELLWIDTH + ColorInfo[FLAG].x,
y * CELLHEIGHT + ColorInfo[FLAG].y) == ColorInfo[FLAG].Color)
{
BoardInfo[x][y] = FLAG;
}
// }
}
}
SelectBitmap(ScreenDC, OldBmp);
DeleteDC(ScreenDC);
for (y = 0; y < CELLCOUNTY; y++)
{
for (x = 0; x < CELLCOUNTX; x++)
{
if (BoardInfo[x][y] >= 0 && BoardInfo[x][y] <= HIGHNUMBER)
{
sprintf(Message, "%d", BoardInfo[x][y]);
TextOut(WindowDC, 10 + 20 * x, 30 + 20 * y, Message, strlen(Message));
}
else if (BoardInfo[x][y] == -1)
{
sprintf(Message, "?");
TextOut(WindowDC, 10 + 20 * x, 30 + 20 * y, Message, strlen(Message));
}
else if (BoardInfo[x][y] == FLAG)
{
sprintf(Message, "F");
TextOut(WindowDC, 10 + 20 * x, 30 + 20 * y, Message, strlen(Message));
}
}
}
POINT pt;
GetCursorPos(&pt);
sprintf(Message, "Rel Pos: (%d, %d) ", pt.x - WindowX, pt.y - WindowY);
TextOut(WindowDC, 10, 30 + 20 * CELLCOUNTY, Message, strlen(Message));
ReleaseDC(MainWindow, WindowDC);
DeleteDC(MainDC);
}
void FindClicks(void)
{
bool RClick[CELLCOUNTX][CELLCOUNTY];
bool LClick[CELLCOUNTX][CELLCOUNTY];
int x, y, i, j;
int OpenCount, FlagCount;
POINT OldPos;
for (y = 0; y < CELLCOUNTY; y++)
{
for (x = 0; x < CELLCOUNTX; x++)
{
LClick[x][y] = false;
RClick[x][y] = false;
}
}
for (y = 0; y < CELLCOUNTY; y++)
{
for (x = 0; x < CELLCOUNTX; x++)
{
if (BoardInfo[x][y] >= 1 && BoardInfo[x][y] <= HIGHNUMBER)
{
OpenCount = 0;
FlagCount = 0;
for (i = MAX(0, x - 1); i <= MIN(CELLCOUNTX - 1, x + 1); i++)
{
for (j = MAX(0, y - 1); j <= MIN(CELLCOUNTY - 1, y + 1); j++)
{
if (BoardInfo[i][j] == -1)
OpenCount++;
if (BoardInfo[i][j] == FLAG)
FlagCount++;
}
}
if (FlagCount == BoardInfo[x][y] && OpenCount > 0)
{
for (i = MAX(0, x - 1); i <= MIN(CELLCOUNTX - 1, x + 1); i++)
{
for (j = MAX(0, y - 1); j <= MIN(CELLCOUNTY - 1, y + 1); j++)
{
if (BoardInfo[i][j] == -1)
LClick[i][j] = true;
}
}
}
if (FlagCount + OpenCount == BoardInfo[x][y] && OpenCount > 0)
{
for (i = MAX(0, x - 1); i <= MIN(CELLCOUNTX - 1, x + 1); i++)
{
for (j = MAX(0, y - 1); j <= MIN(CELLCOUNTY - 1, y + 1); j++)
{
if (BoardInfo[i][j] == -1)
RClick[i][j] = true;
}
}
}
}
}
}
GetCursorPos(&OldPos);
for (y = 0; y < CELLCOUNTY; y++)
{
for (x = 0; x < CELLCOUNTX; x++)
{
if (LClick[x][y])
{
SetCursorPos(WindowX + BOARDX + CELLWIDTH / 2 + CELLWIDTH * x,
WindowY + BOARDY + CELLHEIGHT / 2 + CELLHEIGHT * y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(10);
}
if (RClick[x][y])
{
SetCursorPos(WindowX + BOARDX + CELLWIDTH / 2 + CELLWIDTH * x,
WindowY + BOARDY + CELLHEIGHT / 2 + CELLHEIGHT * y);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
Sleep(5);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
Sleep(5);
}
}
}
SetCursorPos(OldPos.x, OldPos.y);
}
OS: Windows Vista Ultimate 64bit