Рандомное движение курсора мыши вне формы при попадании на пиксель определенного цвета - C#
Формулировка задачи:
Здравствуйте, можно ли реализовать рандомное движение мыши вне формы и нажатие мыши, при наведении на определенный цвет?
Решение задачи: «Рандомное движение курсора мыши вне формы при попадании на пиксель определенного цвета»
textual
Листинг программы
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HDC = System.IntPtr;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
UIntPtr dwExtraInfo);
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
static void Main(string[] args)
{
// Для генерации случайных чисел
Random random = new Random();
// Область, куда не должен попадать курсор
Rectangle rect = new Rectangle(150, 150, 200, 200);
while (true)
{
Point point;
do
{
int x = random.Next(0, Screen.PrimaryScreen.WorkingArea.Width);
int y = random.Next(0, Screen.PrimaryScreen.WorkingArea.Height);
point = new Point(x, y);
}
while (rect.Contains(point));
SetCursorPos(point.X, point.Y);
HDC hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, point.X, point.Y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
// Щелчок только на белом цвете
if (color.A == Color.White.A &&
color.R == Color.White.R &&
color.G == Color.White.G &&
color.B == Color.White.B)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)point.X, (uint)point.Y, 0, UIntPtr.Zero);
Console.WriteLine("Click");
}
}
}
}