剪贴板备份在c#

介绍 代码使用Windows API调用来读取和写入剪贴板。 它提供了一个ClipboardHelper,一个易于使用的API桥接器,此外,它还提供了将复杂的剪贴板数据序列化到硬盘并在需要时恢复它们的功能,使用的是XmlSerializer,而不是只能管理CLR兼容数据的。net框架。 我用ie、Word、OpenOffice进行了测试,包括对图像文件的测试。 背景 . net框架和window . forms。Clipboard类只允许操作可序列化的数据。那么,如果剪贴板包含来自不兼容应用程序的数据,会发生什么呢?简单!. net框架认为剪贴板是空的。 为了读取和操作这些数据,我们需要调用user32.dll和kernerl32.dll。 使用的代码 代码包括两个项目:库ClipboardHelper和控制台应用程序ClipboardTest。 整个代码都是注释的,以理解API的用法和实现的保存过程。 使用这个库非常简单:它是一个公开静态void ClipboardHelper的静态类。序列化函数将剪贴板数据序列化到硬盘,并对静态void进行反序列化以将其反序列化并将其放回剪贴板中,准备粘贴。隐藏,复制Code

//Comment next line to end the demo mode
//and backup your clipboard data.
ClipboardHelper.Deserialize(demo);
Console.WriteLine("restore the demo clipboard");

//Open the clipboard and serialize into a directory
ClipboardHelper.Serialize(fileName);
Console.WriteLine("serialize clipboard to " + fileName);

//Deserialize the clipboard and set data
//to win clipboard ready to be pasted
ClipboardHelper.Deserialize(fileName);
Console.WriteLine("restore the clipboard " + fileName);

测试应用程序显示了运行中的ClipboardHelper示例。它将反序列化剪贴板,在从CodeProject主页上的Internet Explorer中复制一些行之后,将其备份,并从备份中再次反序列化。运行它之后,您已经将剪贴板备份到自己的剪贴板中,准备粘贴。 在ClipboardHelper API调用返回的数据保存在[Serializable]DataClip对象中。它允许轻松地发送剪贴板在远程上下文中,并通过XmlSerializer:Hide  复制Code

public static void SaveToFile(ReadOnlyCollection<DATACLIP> clipData, string clipName)
{
    IEnumerator<DATACLIP> cData = clipData.GetEnumerator();
    while (cData.MoveNext())
    {
        XmlSerializer xml = new XmlSerializer(typeof(DataClip));
        using (StreamWriter sw = new StreamWriter(di.FullName + 
                                 @"\" + i.ToString() + ".cli",false))
        {
            xml.Serialize(sw, cData.Current);
        }
    }
 }

深入剪贴板 剪贴板包含碎片缓冲区中的数据。它们每个都有一个特定的数据格式,剪贴板查看器和用于复制数据的软件使用该格式,以识别如何正确地呈现缓冲区。dll的函数EnumClipboardFormats返回包含在剪贴板中的数据格式数组。对于每个DataFormat,我们可以调用GetClipboardData。它将句柄返回到剪贴板对象。通过从kernel32调用GlobalSize和GlobalLock。dll中,我们获得剪贴板对象的属性(它的长度和指向它的指针),因此我们可以通过封送器将剪贴板对象复制到我们的缓冲区(字节数组)中。复制的方法。 要获得剪贴板,首先,选中打开剪贴板:隐藏复制Code

Win32ClipboardAPI.OpenClipboard(IntPtr.Zero)

然后,我们得到所有的数据格式;查询他们,我们得到剪贴板数据缓冲区:隐藏 收缩,复制Code

    //Init a list of ClipData,
    // which will contain each Clipboard Data
    List<DataClip> clipData = new List<DataClip>();
    //Loop for each clipboard data type
    uint format = 0;
    while ((format = Win32ClipboardAPI.EnumClipboardFormats(format)) != 0)
    {
        //Get the formatName
        StringBuilder formatName = new StringBuilder();
        Win32ClipboardAPI.GetClipboardFormatName(format, 
                                       formatName, 100);
        
        //Get the pointer for the current Clipboard Data 
        IntPtr pos = Win32ClipboardAPI.GetClipboardData(format);
        //Get the clipboard buffer data properties
        UIntPtr lenght = Win32MemoryAPI.GlobalSize(pos);
        IntPtr gLock = Win32MemoryAPI.GlobalLock(pos);
        byte[] buffer;
        //Init a buffer which will contain the clipboard data
        buffer = new byte[(int)lenght];
        int l = Convert.ToInt32(lenght.ToString());
        //Copy data from clipboard to our byte[] buffer
        Marshal.Copy(gLock, buffer, 0, l);
        //Create a ClipData object that 
        //represents the current clipboard data
        DataClip cd = new DataClip(format, 
                      formatName.ToString(), buffer);
        //Add current Clipboard Data to the list
        clipData.Add(cd);
}

历史 08/23/2006 -首次发布。 08/29/2006—整个保存系统已经更改,实现了XmlSerializer。它工作得更好,解决了写文件过程中的bug。 本文转载于:http://www.diyabc.com/frontweb/news168.html

posted @ 2020-08-04 01:32  Dincat  阅读(347)  评论(0编辑  收藏  举报