简体   繁体   English

从不同的应用程序锁定/解锁剪贴板

[英]Lock / unlock clipboard from different applications

I have a two office add-in which communicates using clipboard, for example: copy and paste table or picture from excel to word. 我有一个两个办公室的外接程序,可以使用剪贴板进行通信,例如:将表格或图片从Excel复制并粘贴到Word。 While copy\\paste operation running, user can corrupt data in clipboard if he copy something in clipboard. 在运行复制\\粘贴操作时,如果用户在剪贴板中复制内容,则可能损坏剪贴板中的数据。 I tried lock clipboard after copy in first application and unlock clipboard before paste in second application. 我尝试在第一个应用程序中复制锁定剪贴板 ,并在粘贴到第二个应用程序中之前解锁剪贴板

I can lock Clipboard in one application, but can't unlock it in another application 我可以在一个应用程序中锁定剪贴板,但不能在另一个应用程序中解锁它

This is class with wrappers for Clipboard 这是带有剪贴板包装器的类

public class MyClipboard
    {
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool OpenClipboard(IntPtr hwnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseClipboard();

        public void Close()
        {
            CloseClipboard();
        }

        public void Open()
        {
            OpenClipboard(IntPtr.Zero);
        }
    }

First application : 首次申请:

    MyClipboard clipboard = new MyClipboard();
                clipboard.Open();
    ...

Second application : 二次申请:

 MyClipboard clipboard = new MyClipboard();
                clipboard.Close();
    ...

What should I do to Open clipboard from one application, and close clipboard from another application? 我应该怎么做才能从一个应用程序打开剪贴板,然后从另一个应用程序关闭剪贴板?

Thanks! 谢谢!

Rather than using the clipboard to share data between your apps (as you say the user could modify the clipboard) I'd use something like a 与其使用剪贴板在您的应用之间共享数据(就像您说的用户可以修改剪贴板),我不如使用

Here is someothers ideas.. What is the simplest method of inter-process communication between 2 C# processes? 这是其他一些想法。2个C#进程之间的进程间通信最简单的方法是什么?

If you really want to use the clipboard you could put in some extra custom attributes to the format to make sure that user has not used the clipboard themselves. 如果您确实要使用剪贴板,则可以在格式中添加一些额外的自定义属性,以确保用户自己未使用剪贴板。 But it seems like bad practice to use the clipboard in this way, but that's my opion. 但是以这种方式使用剪贴板似乎是不好的做法,但这是我的选择。

HTH. HTH。 David 大卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM