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(); }