add ColorDropper

This commit is contained in:
NaBian committed 2019-05-14 23:03:14 +08:00
1 parent d920bf678e
commit a8aa1a4525
7 files changed
+180 -25

No files matched your search

@@ -1,7 +1,48 @@
namespace HandyControl.Tools
{
public class MouseHook
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using HandyControl.Data;
using HandyControl.Tools.Interop;
namespace HandyControl.Tools
{
internal class MouseHook
{
public static event EventHandler<MouseHookEventArgs> StatusChanged;
private static IntPtr HookId = IntPtr.Zero;
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
public static void Start() => HookId = SetHook(Proc);
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
private static IntPtr SetHook(UnsafeNativeMethods.HookProc proc)
{
using (var curProcess = Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
if (curModule != null)
{
return UnsafeNativeMethods.SetWindowsHookEx((int) UnsafeNativeMethods.HookType.WH_MOUSE_LL, proc,
UnsafeNativeMethods.GetModuleHandle(curModule.ModuleName), 0);
}
return IntPtr.Zero;
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0) return UnsafeNativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
var hookStruct = (UnsafeNativeMethods.MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(UnsafeNativeMethods.MOUSEHOOKSTRUCT));
StatusChanged?.Invoke(null, new MouseHookEventArgs
{
Message = (MouseHookMessageType)wParam,
Point = new NativeMethods.POINT(hookStruct.pt.X, hookStruct.pt.Y)
});
return UnsafeNativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
}
}
}
@@ -174,5 +174,45 @@ namespace HandyControl.Tools.Interop
[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport(ExternDll.User32, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport(ExternDll.User32, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
public enum HookType
{
WH_MOUSE_LL = 14,
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEHOOKSTRUCT
{
public NativeMethods.POINT pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
}
public delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
[DllImport(ExternDll.User32, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport(ExternDll.User32, SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport(ExternDll.User32, SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport(ExternDll.Gdi32, SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport(ExternDll.User32, SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
}
}