| 1 | using Core.Librarys; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Linq; |
| 5 | using System.Text; |
| 6 | using System.Threading.Tasks; |
| 7 | using System.Windows; |
| 8 | using System.Windows.Controls; |
| 9 | using System.Windows.Input; |
| 10 | |
| 11 | namespace UI.Controls.Base |
| 12 | { |
| 13 | public class ColorSelect : Control |
| 14 | { |
| 15 | public List<string> Colors |
| 16 | { |
| 17 | get { return (List<string>)GetValue(ColorsProperty); } |
| 18 | set { SetValue(ColorsProperty, value); } |
| 19 | } |
| 20 | public static readonly DependencyProperty ColorsProperty = |
| 21 | DependencyProperty.Register("Colors", |
| 22 | typeof(List<string>), |
| 23 | typeof(ColorSelect) |
| 24 | ); |
| 25 | |
| 26 | |
| 27 | public string Color |
| 28 | { |
| 29 | get { return (string)GetValue(ColorProperty); } |
| 30 | set { SetValue(ColorProperty, value); } |
| 31 | } |
| 32 | public static readonly DependencyProperty ColorProperty = |
| 33 | DependencyProperty.Register("Color", |
| 34 | typeof(string), |
| 35 | typeof(ColorSelect), new PropertyMetadata(new PropertyChangedCallback(OnColorPropertyChanged))); |
| 36 | |
| 37 | private static void OnColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 38 | { |
| 39 | var control = d as ColorSelect; |
| 40 | if (string.IsNullOrEmpty(control.Color)) |
| 41 | { |
| 42 | control.Color = control.Colors[0]; |
| 43 | } |
| 44 | control.OnSelected?.Invoke(control, EventArgs.Empty); |
| 45 | } |
| 46 | |
| 47 | public bool IsOpen |
| 48 | { |
| 49 | get { return (bool)GetValue(IsOpenProperty); } |
| 50 | set { SetValue(IsOpenProperty, value); } |
| 51 | } |
| 52 | public static readonly DependencyProperty IsOpenProperty = |
| 53 | DependencyProperty.Register("IsOpen", |
| 54 | typeof(bool), |
| 55 | typeof(ColorSelect), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged))); |
| 56 | |
| 57 | private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 58 | { |
| 59 | var control = d as ColorSelect; |
| 60 | if (e.Property == IsOpenProperty) |
| 61 | { |
| 62 | control.HandleHook(); |
| 63 | } |
| 64 | } |
| 65 | private Win32API.LowLevelKeyboardProc mouseProc; |
| 66 | private static IntPtr hookMouseID = IntPtr.Zero; |
| 67 | private IntPtr hook; |
| 68 | private bool IsFirstClick = false; |
| 69 | private Border SelectContainer; |
| 70 | |
| 71 | |
| 72 | #region hook |
| 73 | private void HandleHook() |
| 74 | { |
| 75 | if (IsOpen) |
| 76 | { |
| 77 | |
| 78 | hook = Win32API.SetMouseHook(mouseProc); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | Win32API.UnhookWindowsHookEx(hook); |
| 83 | } |
| 84 | } |
| 85 | private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) |
| 86 | { |
| 87 | |
| 88 | if (nCode >= 0) |
| 89 | { |
| 90 | if (wParam == (IntPtr)Win32API.WM_LBUTTONDBLCLK || wParam == (IntPtr)Win32API.WM_WHEEL) |
| 91 | { |
| 92 | if (!IsFirstClick) |
| 93 | { |
| 94 | bool isInControl = IsInControl(); |
| 95 | |
| 96 | if (!isInControl) |
| 97 | { |
| 98 | IsOpen = false; |
| 99 | } |
| 100 | } |
| 101 | IsFirstClick = false; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | return Win32API.CallNextHookEx(hookMouseID, nCode, wParam, lParam); |
| 107 | } |
| 108 | private bool IsInControl() |
| 109 | { |
| 110 | var p = Mouse.GetPosition(SelectContainer); |
| 111 | if (p.X < 0 || p.Y < 0 || p.X > SelectContainer.ActualWidth || p.Y > SelectContainer.ActualHeight) |
| 112 | { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | #endregion |
| 120 | |
| 121 | public Command ShowSelectCommand { get; set; } |
| 122 | public Command ColorSelectCommand { get; set; } |
| 123 | public event EventHandler OnSelected; |
| 124 | |
| 125 | public ColorSelect() |
| 126 | { |
| 127 | DefaultStyleKey = typeof(ColorSelect); |
| 128 | ShowSelectCommand = new Command(new Action<object>(OnShowSelect)); |
| 129 | ColorSelectCommand = new Command(new Action<object>(OnColorSelect)); |
| 130 | mouseProc = HookCallback; |
| 131 | |
| 132 | Unloaded += ColorSelect_Unloaded; |
| 133 | LoadColors(); |
| 134 | } |
| 135 | |
| 136 | private void LoadColors() |
| 137 | { |
| 138 | var list = new List<string>() |
| 139 | { |
| 140 | "#00FFAB", |
| 141 | "#017aff", |
| 142 | "#6c47ff", |
| 143 | "#39c0c8", |
| 144 | "#f96300", |
| 145 | "#f34971", |
| 146 | "#ff9382", |
| 147 | "#f5c900", |
| 148 | "#cdad7a", |
| 149 | "#aabb5d", |
| 150 | "#000000", |
| 151 | "#2B20D9" |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | Colors = list; |
| 156 | Color = list[0]; |
| 157 | } |
| 158 | private void OnShowSelect(object obj) |
| 159 | { |
| 160 | IsOpen = true; |
| 161 | IsFirstClick = true; |
| 162 | } |
| 163 | |
| 164 | private string HexConverter(System.Drawing.Color c) |
| 165 | { |
| 166 | return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); |
| 167 | } |
| 168 | |
| 169 | private void OnColorSelect(object obj) |
| 170 | { |
| 171 | IsOpen = false; |
| 172 | IsFirstClick = false; |
| 173 | |
| 174 | System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog(); |
| 175 | if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
| 176 | { |
| 177 | Color = HexConverter(colorDialog.Color); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public override void OnApplyTemplate() |
| 182 | { |
| 183 | base.OnApplyTemplate(); |
| 184 | |
| 185 | SelectContainer = GetTemplateChild("SelectContainer") as Border; |
| 186 | } |
| 187 | |
| 188 | private void ColorSelect_Unloaded(object sender, RoutedEventArgs e) |
| 189 | { |
| 190 | Win32API.UnhookWindowsHookEx(hook); |
| 191 | } |
| 192 | |
| 193 | protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) |
| 194 | { |
| 195 | base.OnMouseLeftButtonDown(e); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | } |
| 200 | } |