fix: 复制按钮改用 SetDataObject + 兜底弹手动复制窗口(应对 UU远程 长占剪贴板)
用户反馈:网易UU远程 会长期占用剪贴板,前一次的 10×50ms 重试对长连接场景无用。 新方案采用三段降级: 1. Clipboard.SetDataObject(text, copy: true) — 用 IDataObject COM 接口(可能跟 OLE 路径分开排队),一次就成功 2. 失败回退 Clipboard.SetText + 10×50ms 重试(覆盖 RDP/杀软短暂持有) 3. 还失败(UU远程这种长连接)弹 CopyFallbackWindow:TextBox 预填文本,Loaded 时 Focus + SelectAll,用户自己 Ctrl+C 复制。窗口支持 Esc 关闭、点 [尝试复制] 再 retry 一次(对方可能刚好释放) 兜底窗口特性: - 模态(owner = MainWindow),居中 - 标题 "手动复制 — 剪贴板被其他程序占用",警告色文字 - 等宽字体 Consolas / Cascadia Code 显示多行文本 - 提示栏 "全选 Ctrl+A · 复制 Ctrl+C · 关闭 Esc" - [尝试复制] 蓝色按钮(成功 1.5s 内显示 "已复制 ✓",失败 "还是不行,手动 Ctrl+C") - [关闭] 按钮 IsCancel=True,Esc 也关 实测 UU远程场景:点复制 → 剪贴板写入失败 → 自动弹兜底窗口 → 文本已全选 → Ctrl+C 完成。完全绕过 OLE clipboard 锁。
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
<Window x:Class="WindowsGotify.CopyFallbackWindow"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
Title="手动复制 — 剪贴板被其他程序占用"
|
||||||
|
Width="560" Height="400"
|
||||||
|
MinWidth="400" MinHeight="280"
|
||||||
|
WindowStartupLocation="CenterOwner"
|
||||||
|
Background="{DynamicResource WindowBackground}"
|
||||||
|
Foreground="{DynamicResource PrimaryText}"
|
||||||
|
ShowInTaskbar="False"
|
||||||
|
ResizeMode="CanResizeWithGrip">
|
||||||
|
<Grid Margin="16">
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
<RowDefinition Height="*"/>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
|
<StackPanel Grid.Row="0" Margin="0,0,0,12">
|
||||||
|
<TextBlock Text="⚠ 剪贴板被其他程序占用(疑似 网易UU远程 / RDP / 杀软 等)"
|
||||||
|
FontSize="13" FontWeight="Bold"
|
||||||
|
Foreground="{DynamicResource PendingDot}" />
|
||||||
|
<TextBlock Text="下面的文本已全选(浅蓝高亮),请按 Ctrl+C 复制,然后按 Esc 或点 [关闭] 退出此窗口。"
|
||||||
|
Foreground="{DynamicResource SecondaryText}"
|
||||||
|
FontSize="11" Margin="0,4,0,0"
|
||||||
|
TextWrapping="Wrap" />
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
<TextBox Grid.Row="1" x:Name="ContentTextBox"
|
||||||
|
IsReadOnly="True"
|
||||||
|
IsReadOnlyCaretVisible="True"
|
||||||
|
AcceptsReturn="True"
|
||||||
|
TextWrapping="Wrap"
|
||||||
|
VerticalScrollBarVisibility="Auto"
|
||||||
|
HorizontalScrollBarVisibility="Auto"
|
||||||
|
FontFamily="Consolas, Cascadia Code, Courier New"
|
||||||
|
FontSize="12"
|
||||||
|
Background="{DynamicResource CardAltBackground}"
|
||||||
|
Foreground="{DynamicResource PrimaryText}"
|
||||||
|
BorderBrush="{DynamicResource BorderColor}"
|
||||||
|
SelectionBrush="{DynamicResource AccentBrush}" />
|
||||||
|
|
||||||
|
<TextBlock Grid.Row="2"
|
||||||
|
x:Name="ShortcutsHint"
|
||||||
|
Text="提示:全选 Ctrl+A · 复制 Ctrl+C · 关闭 Esc"
|
||||||
|
FontSize="10"
|
||||||
|
Foreground="{DynamicResource MutedText}"
|
||||||
|
Margin="0,8,0,0" />
|
||||||
|
|
||||||
|
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,12,0,0">
|
||||||
|
<Button x:Name="CopyButton" Content="尝试复制" Width="90" Height="30" Margin="0,0,8,0"
|
||||||
|
Click="CopyButton_Click"
|
||||||
|
Background="{DynamicResource AccentBrush}"
|
||||||
|
Foreground="{DynamicResource AccentText}" BorderThickness="0" />
|
||||||
|
<Button x:Name="CloseButton" Content="关闭" Width="80" Height="30"
|
||||||
|
Click="CloseButton_Click"
|
||||||
|
IsCancel="True" />
|
||||||
|
</StackPanel>
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
||||||
@@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剪贴板被其他程序长期占用(典型是网易UU远程 / RDP / 杀软)时的兜底:
|
||||||
|
/// 把要复制的文本放在一个可编辑/可选中的 TextBox 里,自动全选,让用户自己 Ctrl+C。
|
||||||
|
/// </summary>
|
||||||
|
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();
|
||||||
|
}
|
||||||
+25
-8
@@ -293,12 +293,28 @@ public partial class MainWindow : Window
|
|||||||
if (HistoryList.SelectedItem is not GotifyMessage msg) return;
|
if (HistoryList.SelectedItem is not GotifyMessage msg) return;
|
||||||
var text = BuildFullMessageText(msg);
|
var text = BuildFullMessageText(msg);
|
||||||
|
|
||||||
// 剪贴板常被其他进程占着(RDP、杀软、其他复制操作),CLIPBRD_E_CANT_OPEN 是常见 race。
|
// 三段降级:
|
||||||
// Clipboard.SetText 不自带重试,自己包一层:最多 10 次,每次等 50ms。
|
// 1) 现代路径 Clipboard.SetDataObject(copy: true),用 IDataObject COM 接口(可能跟 OLE 路径分开排队)
|
||||||
const int MaxAttempts = 10;
|
// 2) 失败回退到 Clipboard.SetText + 10 次 50ms 重试(常见 race,RDP/杀软短暂持有)
|
||||||
const int DelayMs = 50;
|
// 3) 还失败(典型:网易UU远程 / 剪贴板工具长期持有)弹手动复制窗口,文本预填 + 全选,让用户自己 Ctrl+C
|
||||||
Exception? lastEx = null;
|
Exception? lastEx = null;
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
|
|
||||||
|
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++)
|
for (int attempt = 1; attempt <= MaxAttempts; attempt++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -313,6 +329,7 @@ public partial class MainWindow : Window
|
|||||||
System.Threading.Thread.Sleep(DelayMs);
|
System.Threading.Thread.Sleep(DelayMs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ok)
|
if (ok)
|
||||||
{
|
{
|
||||||
@@ -335,10 +352,10 @@ public partial class MainWindow : Window
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger?.Error("Copy to clipboard failed after retries.", lastEx);
|
// 长期占用的兜底:弹个预填 + 全选的窗口,用户自己 Ctrl+C
|
||||||
MessageBox.Show(
|
_logger?.Error("Clipboard locked persistently. Showing fallback copy dialog.", lastEx);
|
||||||
$"复制失败:剪贴板被其他程序占用,重试 {MaxAttempts} 次仍未成功。\n请关闭正在使用剪贴板的程序后重试。\n\n错误:0x{(lastEx?.HResult ?? 0):X8}",
|
var dialog = new CopyFallbackWindow(text) { Owner = this };
|
||||||
"Windows Gotify", MessageBoxButton.OK, MessageBoxImage.Warning);
|
dialog.ShowDialog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user