diff --git a/CopyFallbackWindow.xaml b/CopyFallbackWindow.xaml
new file mode 100644
index 0000000..13ae4a9
--- /dev/null
+++ b/CopyFallbackWindow.xaml
@@ -0,0 +1,61 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CopyFallbackWindow.xaml.cs b/CopyFallbackWindow.xaml.cs
new file mode 100644
index 0000000..8941f44
--- /dev/null
+++ b/CopyFallbackWindow.xaml.cs
@@ -0,0 +1,63 @@
+using System.Windows;
+using System.Windows.Input;
+using System.Windows.Threading;
+
+// 双开 WPF + WinForms 时,这几个类型两个命名空间都有,固定走 WPF 的。
+using Clipboard = System.Windows.Clipboard;
+using KeyEventArgs = System.Windows.Input.KeyEventArgs;
+using Key = System.Windows.Input.Key;
+
+namespace WindowsGotify;
+
+///
+/// 剪贴板被其他程序长期占用(典型是网易UU远程 / RDP / 杀软)时的兜底:
+/// 把要复制的文本放在一个可编辑/可选中的 TextBox 里,自动全选,让用户自己 Ctrl+C。
+///
+public partial class CopyFallbackWindow : Window
+{
+ public CopyFallbackWindow(string text)
+ {
+ InitializeComponent();
+ ContentTextBox.Text = text;
+
+ Loaded += OnLoaded;
+ PreviewKeyDown += OnPreviewKeyDown;
+ }
+
+ private void OnLoaded(object sender, RoutedEventArgs e)
+ {
+ ContentTextBox.Focus();
+ ContentTextBox.SelectAll();
+ }
+
+ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
+ {
+ // Esc 关闭(由 IsCancel 按钮接管)
+ if (e.Key == Key.Escape)
+ {
+ e.Handled = true;
+ Close();
+ }
+ }
+
+ private void CopyButton_Click(object sender, RoutedEventArgs e)
+ {
+ // 再试一次剪贴板(可能对方进程刚好释放了)
+ try
+ {
+ Clipboard.SetDataObject(ContentTextBox.Text, true);
+ CopyButton.Content = "已复制 ✓";
+ var t = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1.5) };
+ t.Tick += (_, _) => { CopyButton.Content = "尝试复制"; t.Stop(); };
+ t.Start();
+ }
+ catch
+ {
+ CopyButton.Content = "还是不行,手动 Ctrl+C";
+ ContentTextBox.Focus();
+ ContentTextBox.SelectAll();
+ }
+ }
+
+ private void CloseButton_Click(object sender, RoutedEventArgs e) => Close();
+}
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 4782b9c..63bf2a7 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -293,24 +293,41 @@ public partial class MainWindow : Window
if (HistoryList.SelectedItem is not GotifyMessage msg) return;
var text = BuildFullMessageText(msg);
- // 剪贴板常被其他进程占着(RDP、杀软、其他复制操作),CLIPBRD_E_CANT_OPEN 是常见 race。
- // Clipboard.SetText 不自带重试,自己包一层:最多 10 次,每次等 50ms。
- const int MaxAttempts = 10;
- const int DelayMs = 50;
+ // 三段降级:
+ // 1) 现代路径 Clipboard.SetDataObject(copy: true),用 IDataObject COM 接口(可能跟 OLE 路径分开排队)
+ // 2) 失败回退到 Clipboard.SetText + 10 次 50ms 重试(常见 race,RDP/杀软短暂持有)
+ // 3) 还失败(典型:网易UU远程 / 剪贴板工具长期持有)弹手动复制窗口,文本预填 + 全选,让用户自己 Ctrl+C
Exception? lastEx = null;
bool ok = false;
- for (int attempt = 1; attempt <= MaxAttempts; attempt++)
+
+ try
{
- try
+ Clipboard.SetDataObject(text, true);
+ ok = true;
+ }
+ catch (Exception ex)
+ {
+ lastEx = ex;
+ _logger?.Warn($"Clipboard.SetDataObject failed: {ex.GetType().Name} 0x{ex.HResult:X8}");
+ }
+
+ if (!ok)
+ {
+ const int MaxAttempts = 10;
+ const int DelayMs = 50;
+ for (int attempt = 1; attempt <= MaxAttempts; attempt++)
{
- Clipboard.SetText(text);
- ok = true;
- break;
- }
- catch (System.Runtime.InteropServices.COMException ex)
- {
- lastEx = ex;
- System.Threading.Thread.Sleep(DelayMs);
+ try
+ {
+ Clipboard.SetText(text);
+ ok = true;
+ break;
+ }
+ catch (System.Runtime.InteropServices.COMException ex)
+ {
+ lastEx = ex;
+ System.Threading.Thread.Sleep(DelayMs);
+ }
}
}
@@ -335,10 +352,10 @@ public partial class MainWindow : Window
}
else
{
- _logger?.Error("Copy to clipboard failed after retries.", lastEx);
- MessageBox.Show(
- $"复制失败:剪贴板被其他程序占用,重试 {MaxAttempts} 次仍未成功。\n请关闭正在使用剪贴板的程序后重试。\n\n错误:0x{(lastEx?.HResult ?? 0):X8}",
- "Windows Gotify", MessageBoxButton.OK, MessageBoxImage.Warning);
+ // 长期占用的兜底:弹个预填 + 全选的窗口,用户自己 Ctrl+C
+ _logger?.Error("Clipboard locked persistently. Showing fallback copy dialog.", lastEx);
+ var dialog = new CopyFallbackWindow(text) { Owner = this };
+ dialog.ShowDialog();
}
}