1. 程式人生 > >C#如何儲存剪貼簿內容,在使用後恢復。

C#如何儲存剪貼簿內容,在使用後恢復。

   C# clipboard類封裝了對剪貼簿的操作,一般使用沒有問題。但由於clipboard封裝的資料型別有限,對於一些自定義型別的剪貼簿資料,如果想佔用剪貼簿並在使用後原樣恢復剪貼簿的資料就會產生問題。試驗了很多方法後,嘗試學習別人C++的思路。使用winapi來處理剪貼本解決問題。

 using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;


namespace 學習
{
    class MyClip
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern IntPtr SetClipboardData(uint Format, IntPtr hData);
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint EnumClipboardFormats(uint format);  //列舉剪貼簿內資料型別
        [DllImport("user32.dll", SetLastError = true)]
        private static extern Int32 OpenClipboard(IntPtr hWndNewOwner);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetClipboardData(uint uFormat);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern Int32 CloseClipboard();
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern Int32 GlobalLock(IntPtr hMem);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern Int32 GlobalUnlock(IntPtr hMem);
        [DllImport("kernel32.dll")]
        public static extern uint GlobalSize(IntPtr hMem);
        /// <summary>
        /// 剪貼簿資料儲存目標資料列表
        /// </summary>
        private static List<byte[]> bytes = new List<byte[]>();
        /// <summary>
        /// 剪貼簿資料型別列表
        /// </summary>
        private static List<uint> formats = new List<uint>();
        /// <summary>
        /// 儲存剪貼簿內容,返回真為成功,false為失敗。
        /// </summary>
        /// <returns></returns>
        public static bool saveClip()
        {
            bool fi = true;
            if (OpenClipboard((IntPtr)0) < 1)
            {
                fi = false;
                return false;
            }
            else
            {
                bytes.Clear();
                formats.Clear();
            }
            IntPtr hMem = IntPtr.Zero;
            IntPtr lpStr = IntPtr.Zero;
            uint uFormat = 0;
            while (fi)
            {
                uFormat = EnumClipboardFormats(uFormat);//列舉剪貼簿所有資料型別
                if (uFormat > 0)
                {
                    formats.Add(uFormat);
                    hMem = GetClipboardData(uFormat);
                    lpStr = (IntPtr)GlobalLock(hMem);
                    if (hMem != (IntPtr)0)
                    {
                        uint size = GlobalSize(hMem);
                        IntPtr s = (IntPtr)GlobalLock(hMem);
                        byte[] buffer = new byte[(int)size];
                        if (size > 0)
                        {
                            Marshal.Copy(s, buffer, 0, (int)size);//將剪貼簿資料儲存到自定義位元組陣列
                        }
                        GlobalUnlock(hMem);
                        bytes.Add(buffer);
                    }
                }
                else
                {
                    fi = false;
                }
            }
            CloseClipboard();
            if (formats.Count>0)
            {return true;}
            else{return false;}
        }
        /// <summary>
        /// 恢復儲存的資料
        /// </summary>
        /// <returns></returns>
        public static bool restoreClip()
        {
            bool fi = true;
            if (formats.Count <= 0) { return false; }
            if (OpenClipboard((IntPtr)0) < 1)
            {
                fi = false;
                return false;
            }
            if (fi)
            {
                for (int i = 0; i < formats.Count; i++)
                {


                    int size = bytes[i].Length;
                    IntPtr si = Marshal.AllocHGlobal(size);
                    if (size > 0)
                    {
                        Marshal.Copy(bytes[i], 0, si, size);
                    }
                    else { }
                    SetClipboardData(formats[i], si);
                }
            }
            CloseClipboard();  
            if (formats.Count>0)
            {return true;}
             else{return false;}
        }


    }
}