如何锁定剪贴板?

发布于 2024-09-04 01:57:32 字数 85 浏览 3 评论 0原文

我想知道是否有办法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗弃M 2024-09-11 01:57:32

OpenClipboard() API 函数(后跟 EmptyClipboard())会锁定剪贴板,直到调用 CloseClipboard。您可能应该在目标进程中传递窗口的窗口句柄,但零可以。

当我想测试我的应用程序是否正常处理锁定剪贴板的恶意应用程序时,这是一个 C# 控制台应用程序,它对我有用。

class Program
{
    [DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool CloseClipboard();

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EmptyClipboard();

    static void Main(string[] args)
    {
        OpenClipboard(new IntPtr(0));
        EmptyClipboard();
        Console.WriteLine("Clipboard locked. Press enter to unlock and exit.");
        Console.ReadLine();
        CloseClipboard();
    }
}

这些声明来自 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.

class Program
{
    [DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool CloseClipboard();

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EmptyClipboard();

    static void Main(string[] args)
    {
        OpenClipboard(new IntPtr(0));
        EmptyClipboard();
        Console.WriteLine("Clipboard locked. Press enter to unlock and exit.");
        Console.ReadLine();
        CloseClipboard();
    }
}

These declarations came from pinvoke.net.

以酷 2024-09-11 01:57:32

不要这样做。剪贴板的存在是为了方便用户,而不是程序员!
您将导致尝试(正确)使用剪贴板的其他程序(包括监视更新的程序)出现错误和崩溃。

“程序不应传输数据
进入我们的剪贴板而不需要
来自的明确指示
用户。”
— Charles Petzold,《Windows 3.1 编程》,微软出版社,1992 年

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

娜些时光,永不杰束 2024-09-11 01:57:32

正如 @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?

蔚蓝源自深海 2024-09-11 01:57:32

不,剪贴板不是这样工作的。如果用户想要复制另一个应用程序中的某些内容,但由于您的应用程序以某种方式锁定了它而无法复制,该怎么办?

如果您尝试让两个进程进行通信,请考虑为进程间通信设计的替代方案,例如远程处理、命名管道、套接字或共享内存。远程处理可能是寻找用 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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文