Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created May 20, 2021 15:24
Show Gist options
  • Save bradmartin333/44f02d4be75ee87ea02c16cc93fb6b9c to your computer and use it in GitHub Desktop.
Save bradmartin333/44f02d4be75ee87ea02c16cc93fb6b9c to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
private static bool contact, lastContact;
private static float hue, lastHue;
static void Main(string[] args)
{
Timer t = new Timer(TimerCallback, null, 0, 250);
Console.ReadLine();
}
private static void TimerCallback(Object o)
{
Color c = GetColorAt(3000, 500);
hue = c.GetHue();
if (Math.Abs(hue - lastHue) >= 20)
contact = true;
else
contact = false;
lastHue = hue;
if (contact != lastContact)
{
Console.WriteLine("Contact " + contact.ToString());
lastContact = contact;
}
GC.Collect();
}
public static Color GetColorAt(int x, int y)
{
IntPtr desk = GetDesktopWindow();
IntPtr dc = GetWindowDC(desk);
int a = (int)GetPixel(dc, x, y);
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment