-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
512 lines (427 loc) · 16.5 KB
/
Program.cs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Media;
using System.Reflection.Emit;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Runtime.Remoting.Contexts;
using System.Xml.Linq;
namespace ColorYoink
{
public static class Program
{
public struct FormatData
{
public uint format;
public string name;
public int length;
public byte[] payload;
public string text;
}
[STAThread]
public static void Main(string[] args)
{
/*List<FormatData> formats = new List<FormatData>();
PInvoke.OpenClipboard(IntPtr.Zero);
uint formatInt = 0;
while ((formatInt = PInvoke.EnumClipboardFormats(formatInt)) != 0)
{
FormatData format = new FormatData();
format.format = formatInt;
StringBuilder formatName = new StringBuilder(256);
PInvoke.GetClipboardFormatName(formatInt, formatName, formatName.Capacity);
format.name = formatName.ToString();
IntPtr hGlobal = PInvoke.GetClipboardData(formatInt);
if (hGlobal == IntPtr.Zero)
{
format.text = "";
format.payload = null;
}
else
{
IntPtr clipboardContentsPointer = PInvoke.GlobalLock(hGlobal);
if (clipboardContentsPointer == IntPtr.Zero)
{
format.text = "";
format.payload = null;
}
else
{
format.text = Marshal.PtrToStringAuto(clipboardContentsPointer);
int size = PInvoke.GlobalSize(hGlobal);
format.payload = new byte[size];
Marshal.Copy(clipboardContentsPointer, format.payload, 0, size);
PInvoke.GlobalUnlock(hGlobal);
}
}
formats.Add(format);
}
// Close the clipboard
PInvoke.CloseClipboard();
Console.ReadLine();
return;*/
GlobalHotkey CtrlAltC = new GlobalHotkey(Keys.C, HotkeyModifier.Control | HotkeyModifier.Alt | HotkeyModifier.NoRepeat, YoinkColor);
CtrlAltC.Register();
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("icon.ico");
notifyIcon.Visible = true;
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("Exit ColorYoink");
exitMenuItem.Click += new EventHandler((object sender, EventArgs e) =>
{
CtrlAltC.Unregister();
notifyIcon.Dispose();
Environment.Exit(0);
});
menu.Items.Add(exitMenuItem);
notifyIcon.ContextMenuStrip = menu;
while (true)
{
Application.DoEvents();
}
}
public static void YoinkColor()
{
try
{
ColorYoink.SetClipboardToPNG(ColorYoink.ColorToBitmap(ColorYoink.GetColorFromScreen(ColorYoink.GetCursorPosition()), 16, 16));
SystemSounds.Asterisk.Play();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public static class ColorYoink
{
#region Public Methods
public static void SetClipboardToString(string source)
{
Clipboard.SetText(source);
}
public static void SetClipboardToImage(Bitmap source)
{
Clipboard.SetImage(source);
}
public static Bitmap clipboardContents = null;
public static void SetClipboardToPNG(Bitmap source)
{
// Open the clipboard
if (!PInvoke.OpenClipboard(IntPtr.Zero))
{
throw new Exception("Failed to open clipboard.");
}
// Empty the clipboard
if (!PInvoke.EmptyClipboard())
{
PInvoke.CloseClipboard();
throw new Exception("Failed to empty clipboard.");
}
// Set the clipboard data to the bitmap
IntPtr hBitmap = source.GetHbitmap();
IntPtr result = PInvoke.SetClipboardData(2 /* CF_BITMAP */, hBitmap);
clipboardContents = source;
// Check if setting the clipboard data was successful
if (result == IntPtr.Zero)
{
PInvoke.CloseClipboard();
throw new Exception("Failed to set clipboard data.");
}
// Close the clipboard
if (!PInvoke.CloseClipboard())
{
throw new Exception("Failed to close clipboard.");
}
while (true)
{
}
}
public static Color GetColorFromScreen(Point point)
{
Bitmap bitmap = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(point.X, point.Y, 0, 0, new Size(1, 1));
graphics.Dispose();
Color output = bitmap.GetPixel(0, 0);
bitmap.Dispose();
return output;
}
public static Color GetColorFromScreen(int x, int y)
{
Bitmap bitmap = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(x, y, 0, 0, new Size(1, 1));
graphics.Dispose();
Color output = bitmap.GetPixel(0, 0);
bitmap.Dispose();
return output;
}
public static Bitmap TakeScreenshot()
{
Rectangle bounds = Screen.AllScreens[0].Bounds;
for (int i = 1; i < Screen.AllScreens.Length; i++)
{
if (Screen.AllScreens[i].Bounds.X < bounds.X)
{
bounds.X = Screen.AllScreens[i].Bounds.X;
}
if (Screen.AllScreens[i].Bounds.Y < bounds.Y)
{
bounds.Y = Screen.AllScreens[i].Bounds.Y;
}
if (Screen.AllScreens[i].Bounds.Right > bounds.Right)
{
bounds.Width = Screen.AllScreens[i].Bounds.Right - bounds.X;
}
if (Screen.AllScreens[i].Bounds.Bottom > bounds.Bottom)
{
bounds.Height = Screen.AllScreens[i].Bounds.Bottom - bounds.Y;
}
}
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
foreach (Screen screen in Screen.AllScreens)
{
graphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, screen.Bounds.X - bounds.X, screen.Bounds.Y - bounds.Y, screen.Bounds.Size);
}
graphics.Dispose();
bitmap.Save("D:\\HI.png");
return bitmap;
}
public static Bitmap TakeScreenshot(int targetScreenIndex)
{
if (targetScreenIndex < 0 || targetScreenIndex >= Screen.AllScreens.Length)
{
throw new Exception("targetScreenIndex must be within the bounds of Screen.AllScreens.");
}
Rectangle bounds = Screen.AllScreens[targetScreenIndex].Bounds;
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size);
graphics.Dispose();
return bitmap;
}
public static Bitmap TakeScreenshot(Screen targetScreen)
{
Rectangle bounds = targetScreen.Bounds;
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size);
graphics.Dispose();
return bitmap;
}
public static Bitmap ColorToBitmap(Color color)
{
Bitmap output = new Bitmap(1, 1);
output.SetPixel(0, 0, color);
return output;
}
public static Bitmap ColorToBitmap(Color color, Size size)
{
Bitmap output = new Bitmap(size.Width, size.Height);
Graphics graphics = Graphics.FromImage(output);
graphics.Clear(color);
graphics.Dispose();
return output;
}
public static Bitmap ColorToBitmap(Color color, int Width, int Height)
{
Bitmap output = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(output);
graphics.Clear(color);
graphics.Dispose();
return output;
}
public static Color HexToColor(string hex)
{
if (hex is null)
{
throw new Exception("hex cannot be null.");
}
if (hex.StartsWith("#"))
{
hex = hex.Substring(1);
}
if (hex.Length == 6)
{
try
{
return Color.FromArgb(255, Convert.ToByte(hex.Substring(0, 2), 16), Convert.ToByte(hex.Substring(2, 2), 16), Convert.ToByte(hex.Substring(4, 2), 16));
}
catch
{
throw new Exception("Invalid hex color.");
}
}
else if (hex.Length == 8)
{
try
{
return Color.FromArgb(Convert.ToByte(hex.Substring(6, 2), 16), Convert.ToByte(hex.Substring(0, 2), 16), Convert.ToByte(hex.Substring(2, 2), 16), Convert.ToByte(hex.Substring(4, 2), 16));
}
catch
{
throw new Exception("Invalid hex color.");
}
}
else
{
throw new Exception("Invalid hex color.");
}
}
public static string ColorToHex(Color color)
{
return $"#{color.R.ToString("X2")}{color.G.ToString("X2")}{color.B.ToString("X2")}";
}
public static Point GetCursorPosition()
{
Point output;
if (!GetCursorPos(out output))
{
throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
}
return output;
}
#endregion
#region Private Methods
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out Point lpPoint);
#endregion
}
public static class PInvoke
{
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseClipboard();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr GetOpenClipboardWindow();
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EmptyClipboard();
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
public static extern uint RegisterClipboardFormat(string lpszFormat);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int GetClipboardFormatName(uint format, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszFormatName, int cchMaxCount);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
public static extern uint EnumClipboardFormats(uint format);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.SysInt)]
public static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int GlobalSize(IntPtr hMem);
}
public static class ClipboardHelper
{
private static NativeWindow _subsystemWindow = new Func<NativeWindow>(() => { NativeWindow subsystemWindow = new NativeWindow(); subsystemWindow.CreateHandle(new CreateParams()); return subsystemWindow; }).Invoke();
public static void OpenClipboard()
{
if (!PInvoke.OpenClipboard(_subsystemWindow.Handle))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public static void CloseClipboard()
{
if (!PInvoke.CloseClipboard())
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public static bool ClipboardOpen()
{
return PInvoke.GetOpenClipboardWindow() != IntPtr.Zero;
}
public static bool ClipboardOpenByMe()
{
return PInvoke.GetOpenClipboardWindow() != _subsystemWindow.Handle;
}
public static bool ClipboardOpenByOther()
{
IntPtr owner = PInvoke.GetOpenClipboardWindow();
return owner != _subsystemWindow.Handle && owner != IntPtr.Zero;
}
public enum ClipboardOpenState : byte { Closed = 0, OpenByMe = 1, OpenByOther = 2 }
public static ClipboardOpenState GetClipboardOpenState()
{
IntPtr owner = PInvoke.GetOpenClipboardWindow();
if (owner == IntPtr.Zero)
{
return ClipboardOpenState.Closed;
}
else if (owner == _subsystemWindow.Handle)
{
return ClipboardOpenState.OpenByMe;
}
else
{
return ClipboardOpenState.OpenByOther;
}
}
public static void ClearClipboard()
{
if (!PInvoke.EmptyClipboard())
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public static void SetClipboardBitmap(Bitmap data)
{
IntPtr hBitmap = data.GetHbitmap();
PInvoke.SetClipboardData(2 /* CF_BITMAP */, hBitmap);
}
public static void SetClipboardData(byte[] data, uint format)
{
IntPtr hMem = Marshal.AllocHGlobal(data.Length);
if (hMem == IntPtr.Zero)
{
throw new Exception("Failed to allocate memory for clipboard");
}
// Copy the bitmap data to the global memory
Marshal.Copy(data, 0, hMem, data.Length);
// Set the clipboard data
PInvoke.SetClipboardData(format, hMem);
}
public static uint GetFormatByName(string name)
{
name = name.ToLower();
for (uint i = 0; i < 1000000; i++)
{
StringBuilder formatName = new StringBuilder(255);
int result = PInvoke.GetClipboardFormatName(i, formatName, formatName.Capacity);
if (!(result is 0) && formatName.ToString().ToLower() == name)
{
return i;
}
}
throw new Exception("Unnable to locate clipboard format with the specified name.");
}
}
}