Skip to content

Instantly share code, notes, and snippets.

@johnlettman-old
Created August 8, 2011 05:17
Show Gist options
  • Save johnlettman-old/1131249 to your computer and use it in GitHub Desktop.
Save johnlettman-old/1131249 to your computer and use it in GitHub Desktop.
Mouse controller
class Controller
{
public static bool InProcess = false;
public static bool RemoteStop = false;
const int PAINT_FROMTOP = 150;
const int PAINT_FRONLEFT = 100;
[DllImport("user32.dll", SetLastError = true)]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int x, int y);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public static void LeftClick(int x, int y)
{
SetCursorPos(x, y);
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
public static void DoPaint(Bitmap image)
{
InProcess = true;
int currentPosX = 1;
int currentPosY = 1;
do
{
if (RemoteStop) break;
Color cTemp = image.GetPixel(currentPosX, currentPosY);
if ((cTemp.R + cTemp.G + cTemp.B) < (127 * 3))
LeftClick((currentPosX + Controller.PAINT_FRONLEFT), (currentPosY + Controller.PAINT_FROMTOP));
else continue;
if ((currentPosX + 1) >= image.Width)
{
currentPosY++;
currentPosX = 1;
}
else
{
if ((currentPosY + 1) >= image.Height) break;
else currentPosX++;
}
System.Threading.Thread.Sleep(2);
} while (true);
InProcess = false;
RemoteStop = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment