如何锁定剪贴板?
我想知道是否有办法从 C# 锁定和解锁剪贴板。基本上,我会在其中写入一些内容,并且在我拿起我的东西之前,我不希望其他人写入它。
我该怎么做?
I was wondering if there is anyway to lock and unlock the clipboard from C#. Basically, I'd be writing something into it and I do not want anyone else to write to it before I pick up my stuff.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OpenClipboard() API 函数(后跟 EmptyClipboard())会锁定剪贴板,直到调用 CloseClipboard。您可能应该在目标进程中传递窗口的窗口句柄,但零可以。
当我想测试我的应用程序是否正常处理锁定剪贴板的恶意应用程序时,这是一个 C# 控制台应用程序,它对我有用。
这些声明来自 pinvoke.net。
The OpenClipboard() API function, followed by EmptyClipboard() locks the clipboard until CloseClipboard is called. You should probably pass a window handle of a window in the target process, but zero works.
Here's a C# console app that worked for me when I wanted to test that my application gracefully handled a rogue application locking the clipboard.
These declarations came from pinvoke.net.
不要这样做。剪贴板的存在是为了方便用户,而不是程序员!
您将导致尝试(正确)使用剪贴板的其他程序(包括监视更新的程序)出现错误和崩溃。
Do NOT do this. The clipboard is there for the convenience of the USER, not the PROGRAMMER!
You will cause errors and crashes in other programs that are trying to (properly) use the clipboard, including programs that are monitoring for updates.
正如 @shambulator 所说,这不是剪贴板的设计目的。也许你应该尝试不同的策略。与遗留系统的集成可以通过多种方式来解决(可惜大多数方式都很糟糕!)。
您只能向其发送击键的系统:它是 Windows 应用程序吗?您能否深入研究其窗口结构,以通过其 HWND在文本框中显式设置文本< /a>?系统是否有您可以使用的任何类型的文件 I/O?也许你可以深入研究它的数据库?
As @shambulator says, this isn't what the clipboard is designed to do. Perhaps you should try a different tack. Integration with legacy systems can be tackled inmany ways (most of them bad, alas!).
The system to which you only can send keystrokes: is it a Windows application? Can you dig into its window structure to explicitly set texts inside text boxes by their HWNDs? Does the system have any kind of file I/O you could use? Perhaps you could dig into its database?
不,剪贴板不是这样工作的。如果用户想要复制另一个应用程序中的某些内容,但由于您的应用程序以某种方式锁定了它而无法复制,该怎么办?
如果您尝试让两个进程进行通信,请考虑为进程间通信设计的替代方案,例如远程处理、命名管道、套接字或共享内存。远程处理可能是寻找用 C# 编写的应用程序的第一个地方。
Nope, the clipboard doesn't work that way. What if the user wanted to copy something in another app, but couldn't because yours had locked it somehow?
If you're trying to have two processes communicate, look into alternatives designed for inter-process communication, like remoting, named pipes, sockets or shared memory. Remoting is probably the first place to look for applications written in C#.