C#或WPF解决第三方工具干扰我们调用Clipboard剪切板问题
场景还原:
当我们在用向日葵时,我们自己调用 Clipboard 这个类时,发现不生效,只有关闭向日葵才能正常复制(该问题存在高版本出现)
需求:
我不想关闭向日葵来达到也能复制文本,怎么办
处理手段:
调用WindowAPI来解决
////// Window帮助类(调用WindpwAPI) /// 创建者:YWK /// 创建时间:2022-06-13 下午 1:10:13 /// public static class WindowHelper { #region 调用WindosAPI C++动态库 [DllImport("User32")] private static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("User32")] private static extern bool CloseClipboard(); [DllImport("User32")] private static extern bool EmptyClipboard(); [DllImport("User32")] private static extern bool IsClipboardFormatAvailable(int format); [DllImport("User32")] private static extern IntPtr GetClipboardData(int uFormat); [DllImport("User32", CharSet = CharSet.Unicode)] private static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem); #endregion /// /// 复制文本 /// /// public static void Copy(string value) { if (!OpenClipboard(IntPtr.Zero)) { Copy(value); return; } EmptyClipboard(); SetClipboardData(13, Marshal.StringToHGlobalUni(value)); CloseClipboard(); } }