-
Notifications
You must be signed in to change notification settings - Fork 42
/
SharpClipboard.cs
97 lines (83 loc) · 3.75 KB
/
SharpClipboard.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
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace SharpClipboard
{
//https://stackoverflow.com/questions/17762037/error-while-trying-to-copy-string-to-clipboard
//https://gist.github.com/glombard/7986317
internal static class NativeMethods
{
//Reference https://docs.microsoft.com/en-us/windows/desktop/dataxchg/wm-clipboardupdate
public const int WM_CLIPBOARDUPDATE = 0x031D;
//Reference https://www.pinvoke.net/default.aspx/Constants.HWND
public static IntPtr HWND_MESSAGE = new IntPtr(-3);
//Reference https://www.pinvoke.net/default.aspx/user32/AddClipboardFormatListener.html
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddClipboardFormatListener(IntPtr hwnd);
//Reference https://www.pinvoke.net/default.aspx/user32.setparent
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
//Reference https://www.pinvoke.net/default.aspx/user32/getwindowtext.html
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
//Reference https://www.pinvoke.net/default.aspx/user32.getwindowtextlength
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
//Reference https://www.pinvoke.net/default.aspx/user32.getforegroundwindow
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
public static class Clipboard
{
public static string GetText()
{
string ReturnValue = string.Empty;
Thread STAThread = new Thread(
delegate ()
{
// Use a fully qualified name for Clipboard otherwise it
// will end up calling itself.
ReturnValue = System.Windows.Forms.Clipboard.GetText();
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();
return ReturnValue;
}
}
public sealed class ClipboardNotification
{
public class NotificationForm : Form
{
string lastWindow = "";
public NotificationForm()
{
//Turn the child window into a message-only window (refer to Microsoft docs)
NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
//Place window in the system-maintained clipboard format listener list
NativeMethods.AddClipboardFormatListener(Handle);
}
protected override void WndProc(ref Message m)
{
//Listen for operating system messages
if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE)
{
//Write to stdout active window
IntPtr active_window = NativeMethods.GetForegroundWindow();
int length = NativeMethods.GetWindowTextLength(active_window);
StringBuilder sb = new StringBuilder(length + 1);
NativeMethods.GetWindowText(active_window, sb, sb.Capacity);
Trace.WriteLine("");
//Write to stdout clipboard contents
Trace.WriteLine("\t[cntrl-C] Clipboard Copied: " + Clipboard.GetText());
}
//Called for any unhandled messages
base.WndProc(ref m);
}
}
}
}