Color Dropper

now we can select color from everywhere
This commit is contained in:
Mahdi Hosseini committed 2019-04-21 10:32:52 +04:30
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);
}
}
@@ -64,6 +64,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\GotoTop.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Gravatar.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Growl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\MouseHook.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\NotifyIcon.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Pagination.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\RadioGroup.cs" />
@@ -22,6 +22,8 @@ namespace HandyControl.Interactivity
/// </summary>
public static RoutedCommand Switch { get; } = new RoutedCommand(nameof(Switch), typeof(ControlCommands));
public static RoutedCommand Dropper { get; } = new RoutedCommand(nameof(Dropper), typeof(ControlCommands));
/// <summary>
/// 右转
/// </summary>
@@ -38,5 +38,6 @@
<Geometry x:Key="AskGeometry">M512 0 30.11843 240.941297l0 542.117406 481.88157 240.941297 481.88157-240.941297L993.88157 240.941297 512 0zM575.776472 768.799969 460.188012 768.799969 460.188012 656.222073l115.588459 0L575.776472 768.799969zM623.335603 509.329685c-52.375829 36.723353-59.600363 55.988096-59.600363 84.885211l0 19.866447L468.616977 614.081343l0-26.489278c0-45.754021 13.846342-80.67124 61.406497-116.791866 46.957428-36.723353 57.79423-62.0082 57.79423-84.282484 0-25.284848-21.67258-54.181962-55.386393-54.181962-42.743457 0-70.436142 26.489278-82.477374 85.486914l-105.956088-21.67258c24.683144-111.976192 82.477374-157.127486 205.289345-157.127486 98.12985 0 157.72919 63.212631 157.72919 131.842639C707.017407 423.240044 688.956071 461.76953 623.335603 509.329685z</Geometry>
<Geometry x:Key="AllGeometry">M 721.005 638.949 C 758.74 638.949 789.333 669.56 789.333 707.312 C 789.333 745.047 758.74 775.657 721.005 775.657 C 683.253 775.657 652.66 745.047 652.66 707.312 C 652.66 669.56 683.253 638.949 721.005 638.949 Z M 512.017 638.949 C 549.77 638.949 580.38 669.56 580.38 707.312 C 580.38 745.047 549.77 775.657 512.017 775.657 C 474.265 775.657 443.672 745.047 443.672 707.312 C 443.672 669.56 474.265 638.949 512.017 638.949 Z M 303.029 638.949 C 340.781 638.949 371.392 669.56 371.392 707.312 C 371.392 745.047 340.781 775.657 303.029 775.657 C 265.277 775.657 234.667 745.047 234.667 707.312 C 234.667 669.56 265.277 638.949 303.029 638.949 Z M 721.005 443.655 C 758.74 443.655 789.333 474.265 789.333 512.017 C 789.333 549.769 758.74 580.363 721.005 580.363 C 683.253 580.363 652.66 549.769 652.66 512.017 C 652.66 474.265 683.253 443.655 721.005 443.655 Z M 512.017 443.655 C 549.77 443.655 580.38 474.265 580.38 512.017 C 580.38 549.769 549.77 580.363 512.017 580.363 C 474.265 580.363 443.672 549.769 443.672 512.017 C 443.672 474.265 474.265 443.655 512.017 443.655 Z M 303.029 443.655 C 340.781 443.655 371.392 474.265 371.392 512.017 C 371.392 549.769 340.781 580.363 303.029 580.363 C 265.277 580.363 234.667 549.769 234.667 512.017 C 234.667 474.265 265.277 443.655 303.029 443.655 Z M 721.005 248.343 C 758.74 248.343 789.333 278.953 789.333 316.688 C 789.333 354.44 758.74 385.051 721.005 385.051 C 683.253 385.051 652.66 354.44 652.66 316.688 C 652.66 278.953 683.253 248.343 721.005 248.343 Z M 512.017 248.343 C 549.77 248.343 580.38 278.953 580.38 316.688 C 580.38 354.44 549.77 385.051 512.017 385.051 C 474.265 385.051 443.672 354.44 443.672 316.688 C 443.672 278.953 474.265 248.343 512.017 248.343 Z M 303.029 248.343 C 340.781 248.343 371.392 278.953 371.392 316.688 C 371.392 354.44 340.781 385.051 303.029 385.051 C 265.277 385.051 234.667 354.44 234.667 316.688 C 234.667 278.953 265.277 248.343 303.029 248.343 Z</Geometry>
<Geometry x:Key="DragGeometry">M384 896C384 825.307552 440.814697 768 512 768 582.692448 768 640 824.814694 640 896 640 966.692448 583.185303 1024 512 1024 441.307552 1024 384 967.185306 384 896ZM384 512C384 441.307552 440.814697 384 512 384 582.692448 384 640 440.814697 640 512 640 582.692448 583.185303 640 512 640 441.307552 640 384 583.185303 384 512ZM384 128C384 57.307552 440.814697 0 512 0 582.692448 0 640 56.814697 640 128 640 198.692448 583.185303 256 512 256 441.307552 256 384 199.185303 384 128Z</Geometry>
<Geometry x:Key="DropperGeometry">M 416 32 C 399.625 32 383.25 38.25 370.625 50.75 C 370.625 50.75 312.75 109.5 270.75 152.125 L 246.5 127.875 L 223.875 150.5 L 248.25 174.875 C 173.375 250.875 101.5 323.875 84.75 340.625 C 67.25 358.125 65.75 374.125 64.625 386.875 C 63.375 400.125 62.375 411.5 43 430.875 L 31.75 442.125 L 69.625 480.125 L 80.875 468.75 C 99.75 450 110.375 449.25 122.875 448.375 C 136 447.5 152.25 446.375 171.25 427.375 C 189.625 409 261.5 338.25 337.125 263.75 L 361.5 288.125 L 384.125 265.5 L 359.875 241.25 C 402.25 199.5 461.25 141.25 461.25 141.25 C 486.125 116.25 486.125 75.75 461.25 50.75 C 448.75 38.25 432.375 32 416 32 Z M 416 64 C 424.125 64 432.375 67.125 438.625 73.5 C 451.125 85.875 451.125 106.125 438.75 118.5 C 438.75 118.5 379.625 176.875 337.25 218.625 L 293.375 174.75 C 335.375 132.125 393.375 73.375 393.375 73.375 C 399.625 67.125 407.875 64 416 64 Z M 270.875 197.5 L 314.5 241.125 C 238.875 315.625 167 386.375 148.75 404.625 C 138.125 415.25 131.625 415.75 120.75 416.5 C 111.375 417.125 100.625 417.875 87.75 424.125 C 94.625 410.375 95.625 399 96.5 389.75 C 97.5 378.625 98 372.625 107.25 363.375 C 124.125 346.5 195.875 273.5 270.875 197.5 Z</Geometry>
</ResourceDictionary>
@@ -111,4 +111,7 @@
<Setter Property="Data" Value="{StaticResource UpDownGeometry}"/>
</Style>
<Style x:Key="DropperPathStyle" BasedOn="{StaticResource PathBaseStyle}" TargetType="Path">
<Setter Property="Data" Value="{StaticResource DropperGeometry}"/>
</Style>
</ResourceDictionary>
@@ -93,11 +93,18 @@
<TextBlock Foreground="{DynamicResource ThirdlyTextBrush}" Margin="0,6,0,0" FontSize="10" Text="A" HorizontalAlignment="Center"/>
</StackPanel>
</UniformGrid>
<Button Command="interactivity:ControlCommands.Switch" Padding="6" FontSize="8" BorderThickness="0" HorizontalAlignment="Right" Height="24" Width="20" Grid.Column="2" Grid.Row="1">
<Button.Content>
<Path Fill="#323534" Style="{StaticResource UpDownPathStyle}"/>
</Button.Content>
</Button>
<UniformGrid Rows="2" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="1">
<Button Command="interactivity:ControlCommands.Switch" Padding="6" FontSize="8" BorderThickness="0" Height="24" Width="20">
<Button.Content>
<Path Fill="#323534" Style="{StaticResource UpDownPathStyle}"/>
</Button.Content>
</Button>
<Button Command="interactivity:ControlCommands.Dropper" Padding="6" FontSize="8" BorderThickness="0" Height="24" Width="20">
<Button.Content>
<Path Fill="#323534" Style="{StaticResource DropperPathStyle}"/>
</Button.Content>
</Button>
</UniformGrid>
</Grid>
<Border Grid.Row="2" BorderThickness="0,1,0,0" BorderBrush="{DynamicResource BorderBrush}">
<WrapPanel Name="PART_PanelColor" Margin="12,12,0,12" HorizontalAlignment="Center"/>