|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Text; |
| 3 | +using Microsoft.UI.Xaml; |
| 4 | +using WinRT.Interop; |
| 5 | + |
| 6 | +namespace NoteUI; |
| 7 | + |
| 8 | +internal sealed class ClipboardMonitor : IDisposable |
| 9 | +{ |
| 10 | + private readonly IntPtr _ownHwnd; |
| 11 | + |
| 12 | + public string? SourceExePath { get; private set; } |
| 13 | + public string? SourceTitle { get; private set; } |
| 14 | + |
| 15 | + public ClipboardMonitor(Window window) |
| 16 | + { |
| 17 | + _ownHwnd = WindowNative.GetWindowHandle(window); |
| 18 | + Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged += OnContentChanged; |
| 19 | + } |
| 20 | + |
| 21 | + private void OnContentChanged(object? sender, object e) |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + var fgHwnd = GetForegroundWindow(); |
| 26 | + if (fgHwnd == IntPtr.Zero || fgHwnd == _ownHwnd) return; |
| 27 | + |
| 28 | + GetWindowThreadProcessId(fgHwnd, out var pid); |
| 29 | + if (pid == 0) return; |
| 30 | + |
| 31 | + SourceExePath = GetProcessPath(pid); |
| 32 | + |
| 33 | + var sb = new StringBuilder(512); |
| 34 | + GetWindowText(fgHwnd, sb, sb.Capacity); |
| 35 | + SourceTitle = sb.ToString(); |
| 36 | + } |
| 37 | + catch { } |
| 38 | + } |
| 39 | + |
| 40 | + private static string? GetProcessPath(uint pid) |
| 41 | + { |
| 42 | + const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; |
| 43 | + var handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid); |
| 44 | + if (handle == IntPtr.Zero) return null; |
| 45 | + try |
| 46 | + { |
| 47 | + var sb = new StringBuilder(1024); |
| 48 | + int size = sb.Capacity; |
| 49 | + return QueryFullProcessImageName(handle, 0, sb, ref size) ? sb.ToString() : null; |
| 50 | + } |
| 51 | + finally |
| 52 | + { |
| 53 | + CloseHandle(handle); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static string CleanTitle(string? title, string? exePath) |
| 58 | + { |
| 59 | + if (string.IsNullOrEmpty(title)) return ""; |
| 60 | + |
| 61 | + var processName = string.IsNullOrEmpty(exePath) |
| 62 | + ? "" |
| 63 | + : Path.GetFileNameWithoutExtension(exePath).ToLowerInvariant(); |
| 64 | + |
| 65 | + if (processName is "chrome" or "msedge" or "firefox" or "brave" or "opera" or "vivaldi" or "arc") |
| 66 | + { |
| 67 | + string[] suffixes = |
| 68 | + [ |
| 69 | + " - Google Chrome", " \u2014 Mozilla Firefox", " - Mozilla Firefox", |
| 70 | + " - Microsoft\u200B Edge", " - Microsoft Edge", |
| 71 | + " - Brave", " - Opera", " - Vivaldi", " - Arc" |
| 72 | + ]; |
| 73 | + foreach (var suffix in suffixes) |
| 74 | + { |
| 75 | + if (title.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) |
| 76 | + return title[..^suffix.Length]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return title; |
| 81 | + } |
| 82 | + |
| 83 | + public void Dispose() |
| 84 | + { |
| 85 | + Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged -= OnContentChanged; |
| 86 | + } |
| 87 | + |
| 88 | + [DllImport("user32.dll")] |
| 89 | + private static extern IntPtr GetForegroundWindow(); |
| 90 | + |
| 91 | + [DllImport("user32.dll")] |
| 92 | + private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); |
| 93 | + |
| 94 | + [DllImport("user32.dll", CharSet = CharSet.Unicode)] |
| 95 | + private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount); |
| 96 | + |
| 97 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 98 | + private static extern bool QueryFullProcessImageName(IntPtr hProcess, int flags, StringBuilder buffer, ref int size); |
| 99 | + |
| 100 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 101 | + private static extern IntPtr OpenProcess(uint access, bool inheritHandle, uint processId); |
| 102 | + |
| 103 | + [DllImport("kernel32.dll")] |
| 104 | + private static extern bool CloseHandle(IntPtr handle); |
| 105 | +} |
0 commit comments