-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClipboardMonitor.cs
More file actions
105 lines (85 loc) · 3.35 KB
/
ClipboardMonitor.cs
File metadata and controls
105 lines (85 loc) · 3.35 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
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.UI.Xaml;
using WinRT.Interop;
namespace NoteUI;
internal sealed class ClipboardMonitor : IDisposable
{
private readonly IntPtr _ownHwnd;
public string? SourceExePath { get; private set; }
public string? SourceTitle { get; private set; }
public ClipboardMonitor(Window window)
{
_ownHwnd = WindowNative.GetWindowHandle(window);
Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged += OnContentChanged;
}
private void OnContentChanged(object? sender, object e)
{
try
{
var fgHwnd = GetForegroundWindow();
if (fgHwnd == IntPtr.Zero || fgHwnd == _ownHwnd) return;
GetWindowThreadProcessId(fgHwnd, out var pid);
if (pid == 0) return;
SourceExePath = GetProcessPath(pid);
var sb = new StringBuilder(512);
GetWindowText(fgHwnd, sb, sb.Capacity);
SourceTitle = sb.ToString();
}
catch { }
}
private static string? GetProcessPath(uint pid)
{
const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
var handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
if (handle == IntPtr.Zero) return null;
try
{
var sb = new StringBuilder(1024);
int size = sb.Capacity;
return QueryFullProcessImageName(handle, 0, sb, ref size) ? sb.ToString() : null;
}
finally
{
CloseHandle(handle);
}
}
public static string CleanTitle(string? title, string? exePath)
{
if (string.IsNullOrEmpty(title)) return "";
var processName = string.IsNullOrEmpty(exePath)
? ""
: Path.GetFileNameWithoutExtension(exePath).ToLowerInvariant();
if (processName is "chrome" or "msedge" or "firefox" or "brave" or "opera" or "vivaldi" or "arc")
{
string[] suffixes =
[
" - Google Chrome", " \u2014 Mozilla Firefox", " - Mozilla Firefox",
" - Microsoft\u200B Edge", " - Microsoft Edge",
" - Brave", " - Opera", " - Vivaldi", " - Arc"
];
foreach (var suffix in suffixes)
{
if (title.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
return title[..^suffix.Length];
}
}
return title;
}
public void Dispose()
{
Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged -= OnContentChanged;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool QueryFullProcessImageName(IntPtr hProcess, int flags, StringBuilder buffer, ref int size);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(uint access, bool inheritHandle, uint processId);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr handle);
}