Color Dropper
now we can select color from everywhere
This commit is contained in:
1 parent
89319eadee
commit
3093d17db8
7 files changed
+156
-5
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
@@ -337,6 +338,7 @@ namespace HandyControl.Controls
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, ButtonConfirm_OnClick));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Switch, ButtonSwitch_OnClick));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Cancel, ButtonCancel_OnClick));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Dropper, ButtonDropper_OnClick));
|
||||
|
||||
Loaded += (s, e) =>
|
||||
{
|
||||
@@ -678,5 +680,38 @@ namespace HandyControl.Controls
|
||||
public void Dispose() => System.Windows.Window.GetWindow(this)?.Close();
|
||||
|
||||
public bool CanDispose { get; } = true;
|
||||
|
||||
private void ButtonDropper_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
MouseHook.Start();
|
||||
MouseHook.MouseAction += MouseHook_MouseAction;
|
||||
}
|
||||
|
||||
private void MouseHook_MouseAction(object sender, MouseHook.RawMouseEventArgs e)
|
||||
{
|
||||
SelectedBrush = new SolidColorBrush(GetColorAt(e.Point.x, e.Point.y));
|
||||
if (e.Message == MouseHook.MouseMessages.WM_LBUTTONDOWN)
|
||||
{
|
||||
MouseHook.Stop();
|
||||
MouseHook.MouseAction -= MouseHook_MouseAction;
|
||||
}
|
||||
}
|
||||
[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);
|
||||
|
||||
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, (byte)((a >> 0) & 0xff), (byte)((a >> 8) & 0xff), (byte)((a >> 16) & 0xff));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HandyControl
|
||||
{
|
||||
public static class MouseHook
|
||||
{
|
||||
//public static event EventHandler MouseAction = delegate { };
|
||||
public static event EventHandler<RawMouseEventArgs> MouseAction = delegate { };
|
||||
public class RawMouseEventArgs : EventArgs
|
||||
{
|
||||
internal MouseMessages Message { get; set; }
|
||||
internal POINT Point { get; set; }
|
||||
}
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
_hookID = SetHook(_proc);
|
||||
}
|
||||
public static void Stop()
|
||||
{
|
||||
UnhookWindowsHookEx(_hookID);
|
||||
}
|
||||
|
||||
private static LowLevelMouseProc _proc = HookCallback;
|
||||
private static IntPtr _hookID = IntPtr.Zero;
|
||||
|
||||
private static IntPtr SetHook(LowLevelMouseProc proc)
|
||||
{
|
||||
using (Process curProcess = Process.GetCurrentProcess())
|
||||
using (ProcessModule curModule = curProcess.MainModule)
|
||||
{
|
||||
return SetWindowsHookEx(WH_MOUSE_LL, proc,
|
||||
GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private static IntPtr HookCallback(
|
||||
int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode < 0) return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
||||
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
|
||||
MouseAction(null, new RawMouseEventArgs() { Message = (MouseMessages)wParam, Point = hookStruct.pt });
|
||||
|
||||
return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
||||
}
|
||||
|
||||
private const int WH_MOUSE_LL = 14;
|
||||
|
||||
public enum MouseMessages
|
||||
{
|
||||
WM_LBUTTONDOWN = 0x0201,
|
||||
WM_LBUTTONUP = 0x0202,
|
||||
WM_MOUSEMOVE = 0x0200,
|
||||
WM_MOUSEWHEEL = 0x020A,
|
||||
WM_RBUTTONDOWN = 0x0204,
|
||||
WM_RBUTTONUP = 0x0205
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct POINT
|
||||
{
|
||||
public int x;
|
||||
public int y;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MSLLHOOKSTRUCT
|
||||
{
|
||||
public POINT pt;
|
||||
public uint mouseData;
|
||||
public uint flags;
|
||||
public uint time;
|
||||
public IntPtr dwExtraInfo;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern IntPtr SetWindowsHookEx(int idHook,
|
||||
LowLevelMouseProc 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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user