-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
304 lines (266 loc) · 10.8 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
304 lines (266 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Windows.Media;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;
using DrawingColorConverter = System.Drawing.ColorConverter;
using DrawingColor = System.Drawing.Color;
using PickerOptions;
using ColorPicker;
namespace ColorPicker
{
public partial class MainWindow : Window
{
private DispatcherTimer _updateTimer; // Guessing its set to 0ms so the window tracks the cursor smoothly
private NotifyIcon _notifyIcon;
private const int HOTKEY_ID = 9000;
private const int ESCAPE_HOTKEY_ID = 9001;
private const uint MOD_CONTROL = 0x0002;
private const uint MOD_SHIFT = 0x0004;
private const uint VK_C = 0x43;
private const uint VK_ESCAPE = 0x1B;
private readonly ZoomWindow _zoomWindow;
private bool isZoomWindowVisible = false;
private readonly IntPtr _hookID;
private const int WH_MOUSE_LL = 14;
public string copiedHexCode;
public PickerOptionsWindow _pickerOptionsWindow;
[StructLayout(LayoutKind.Sequential)]
private struct POINT // tf even is this
{
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;
}
// Dont ask me where i got ts shi from :pray:
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
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);
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private readonly LowLevelMouseProc _proc;
public MainWindow()
{
InitializeComponent();
_zoomWindow = new ZoomWindow { Visibility = Visibility.Collapsed }; // Initially collapsed. Wont display if the MainWindow is collapsed too
_proc = HookCallback;
_hookID = SetHook(_proc);
CreateNotifyIcon();
RegisterHotKeys();
_pickerOptionsWindow = new PickerOptionsWindow();
this.Loaded += (s, e) =>
{
this.Hide();
_updateTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(0)
};
_updateTimer.Tick += UpdateColorDisplay;
_updateTimer.Start();
};
this.StateChanged += (s, e) =>
{
if (this.WindowState == WindowState.Minimized)
{
this.Hide();
if (isZoomWindowVisible)
{
_zoomWindow.Visibility = Visibility.Collapsed;
isZoomWindowVisible = false;
}
}
};
}
// Displays the ColorPicker
public void ShowApp()
{
this.Show();
this.WindowState = WindowState.Normal;
this.Activate();
}
private void ColorCodeTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
CopyColorCodeToClipboard();
}
// Copies text to clipboard
private void CopyColorCodeToClipboard()
{
copiedHexCode = ColorCodeTextBlock.Text;
System.Windows.Forms.Clipboard.SetText(copiedHexCode);
ShowPickerOptions();
_pickerOptionsWindow.UpdateColors(copiedHexCode);
}
// Displays after the MainWindow is collapsed
private void ShowPickerOptions()
{
if (_pickerOptionsWindow != null && _pickerOptionsWindow.IsVisible)
{
_pickerOptionsWindow.UpdateHexLabel(copiedHexCode);
}
else
{
_pickerOptionsWindow = new PickerOptionsWindow();
_pickerOptionsWindow.Closed += (s, e) => _pickerOptionsWindow = null;
_pickerOptionsWindow.UpdateHexLabel(copiedHexCode);
_pickerOptionsWindow.Show();
}
_pickerOptionsWindow.ShowColorPickerPanel();
_pickerOptionsWindow.ShowColorEditorPanelColl();
}
private IntPtr SetHook(LowLevelMouseProc proc)
{
using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
// Logic to display/collapse ZoomWindow
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
const int WM_MOUSEWHEEL = 0x020A;
const int WM_LBUTTONDOWN = 0x0201;
if (nCode >= 0)
{
var hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if ((int)wParam == WM_MOUSEWHEEL)
{
int wheelDelta = (short)((hookStruct.mouseData >> 16) & 0xFFFF);
if (wheelDelta > 0 && this.Visibility == Visibility.Visible && !isZoomWindowVisible)
{
_zoomWindow.Topmost = false;
_zoomWindow.Show();
_zoomWindow.Visibility = Visibility.Visible;
UpdateZoomWindowPosition(hookStruct.pt);
isZoomWindowVisible = true;
}
else if (wheelDelta < 0 && isZoomWindowVisible)
{
_zoomWindow.Visibility = Visibility.Collapsed;
isZoomWindowVisible = false;
}
}
else if ((int)wParam == WM_LBUTTONDOWN)
{
if (this.Visibility == Visibility.Visible)
{
CopyColorCodeToClipboard();
MinimizeToTray();
}
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
protected override void OnClosed(EventArgs e)
{
UnhookWindowsHookEx(_hookID);
base.OnClosed(e);
}
// Adds a menu for the application in the system tray
private void CreateNotifyIcon()
{
_notifyIcon = new NotifyIcon
{
Icon = new Icon(SystemIcons.Application, 40, 40),
Visible = true,
ContextMenuStrip = new ContextMenuStrip
{
Items =
{
new ToolStripMenuItem("Exit", null, Exit_Click)
}
}
};
_notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e) { ShowApp(); }
private void Settings_Click(object sender, EventArgs e) { System.Windows.Forms.MessageBox.Show("Settings clicked."); }
private void Exit_Click(object sender, EventArgs e) { System.Windows.Application.Current.Shutdown(); }
private void RegisterHotKeys()
{
var helper = new WindowInteropHelper(_zoomWindow)
{
Owner = new WindowInteropHelper(this).Handle
};
RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CONTROL | MOD_SHIFT, VK_C);
RegisterHotKey(helper.Handle, ESCAPE_HOTKEY_ID, 0, VK_ESCAPE);
ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
}
private void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
if (msg.message == WM_HOTKEY)
{
if ((int)msg.wParam == HOTKEY_ID)
{
ShowApp();
handled = true;
}
else if ((int)msg.wParam == ESCAPE_HOTKEY_ID)
{
MinimizeToTray();
if (isZoomWindowVisible)
{
_zoomWindow.Visibility = Visibility.Collapsed;
isZoomWindowVisible = false;
}
handled = true;
}
}
}
private void MinimizeToTray()
{
this.WindowState = WindowState.Minimized;
this.Hide();
if (isZoomWindowVisible)
{
_zoomWindow.Visibility = Visibility.Collapsed;
isZoomWindowVisible = false;
}
}
private void UpdateZoomWindowPosition(POINT cursorPosition)
{
double offsetX = -25;
double offsetY = -25;
_zoomWindow.Left = cursorPosition.x + offsetX;
_zoomWindow.Top = cursorPosition.y + offsetY;
}
// Logic to Display Color
private void UpdateColorDisplay(object sender, EventArgs e)
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
this.Left = cursorPosition.X + 15;
this.Top = cursorPosition.Y - 25;
using (var bitmap = new Bitmap(1, 1))
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(cursorPosition.X, cursorPosition.Y, 0, 0, new System.Drawing.Size(1, 1));
var pixelColor = bitmap.GetPixel(0, 0);
var color = System.Windows.Media.Color.FromArgb(pixelColor.A, pixelColor.R, pixelColor.G, pixelColor.B);
ColorDisplay.Background = new SolidColorBrush(color);
ColorCodeTextBlock.Text = $"{color.R:X2}{color.G:X2}{color.B:X2}".ToLower();
}
}
}
}