How to lock the clipboard?

2019-04-09 18:03发布

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?

标签: c# clipboard
4条回答
Juvenile、少年°
2楼-- · 2019-04-09 18:36

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?

查看更多
在下西门庆
3楼-- · 2019-04-09 18:40

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. No idea if this will actually work. Visit pinvoke.net for the required declarations.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-09 18:42

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.

“Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.”
— Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992

查看更多
放荡不羁爱自由
5楼-- · 2019-04-09 18:42

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#.

查看更多
登录 后发表回答