首先请注意,这在您的应用程序中可能不是问题。其他应用程序锁定了剪贴板或弄乱了通知链,现在您的应用程序无法访问它。当我确实遇到这样的问题时,我重新启动计算机,它们神奇地消失了……嗯……至少在我再次运行产生问题的应用程序之前。
此代码(未在 Delphi 中检查)可能会对您有所帮助。它不会解决问题,因为通知链已损坏(除了 PC 重新启动之外,什么都无法解决),但如果应用程序锁定剪贴板一段时间,它将解决问题。如果讨厌的应用程序将剪贴板锁定很长时间(秒),请增加 MaxRetries:
procedure Str2Clipboard(CONST Str: string; iDelayMs: integer);
CONST
MaxRetries= 5;
VAR RetryCount: Integer;
begin
RetryCount:= 0;
for RetryCount:= 1 to MaxRetries DO
TRY
inc(RetryCount);
Clipboard.AsText:= Str;
Break;
EXCEPT
on Exception DO
if RetryCount = MaxRetries
then RAISE Exception.Create('Cannot set clipboard')
else Sleep(iDelayMs)
END;
end;
此外,删除“raise”并将其转换为函数并像这样使用它可能是个好主意:
if not Str2Clipboard
then Log.AddMsg('Dear user, other applications are blocking the clipboard. We have tried. We really did. But it didn''t work. Try again in a few seconds.');