using System;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace YetAnotherColorPicker
{
public partial class Form1 : Form
{
private const int WH_KEYBOARD_LL = 13, WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
int x, y;
Color color;
Thread t;
public static Thread timer;
private bool pickerStartet = false;
public Form1()
{
InitializeComponent();
instance = this;
_hookID = SetHook(_proc);
}
public void onKeyDown(int keyCode)
{
//Debug
MessageBox.Show(keyCode);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private static Form1 instance;
public static Form1 Instance
{
get { return instance; }
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
Instance.onKeyDown(Marshal.ReadInt32(lParam));
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = pickColorPanel;
//comboBox1.SelectedIndex = 0;
//this.t.Start();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void AppendTextLabel()
{
if(InvokeRequired)
{
this.Invoke(new Action(AppendTextLabel));
return;
}
coordsLabel.Text = "X: " + x.ToString() + " Y: " + y.ToString(); ;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.pickerStartet == true)
{
this.t.Abort();
}
}
private void schließenToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void neustartenToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
if (this.pickerStartet == true)
{
this.t.Abort();
this.pickerStartet = false;
}
else
{
this.t = new Thread(update);
this.t.Start();
this.pickerStartet = true;
}
}
private void update()
{
Bitmap screenCopy = new Bitmap(1, 1);
using (Graphics gdest = Graphics.FromImage(screenCopy))
{
while (true)
{
x = Cursor.Position.X;
y = Cursor.Position.Y;
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
color = Color.FromArgb(screenCopy.GetPixel(0, 0).ToArgb());
pickColorPanel.BackColor = color;
AppendTextLabel();
Thread.Sleep(50);
//MessageBox.Show(c.ToString());
}
}
}
}
}
Alles anzeigen