public static extern IntPtr GlobalSize(IntPtr handle);

        static extern IntPtr GlobalLock(IntPtr hMem);

        static extern IntPtr GetClipboardData(uint uFormat);

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

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

        const uint CF_TEXT = 1;
        const uint CF_UNICODETEXT = 13;
        const uint CF_OEMTEXT = 7;

        /// <summary>
        /// Gets the data on the clipboard in the format specified by
        /// </summary>
        public static string GetClipboardData()

            //Get pointer to clipboard data in the selected format
            IntPtr ClipboardDataPointer = GetClipboardData(CF_TEXT);

            //Do a bunch of crap necessary to copy the data from the memory
            //the above pointer points at to a place we can access it.
            IntPtr Length = GlobalSize(ClipboardDataPointer);
            IntPtr gLock = GlobalLock(ClipboardDataPointer);

            //Init a buffer which will contain the clipboard data
            byte[] Buffer = new byte[(int)Length];

            //Copy clipboard data to buffer
            Marshal.Copy(gLock, Buffer, 0, (int)Length);

            return System.Text.UnicodeEncoding.Default.GetString(Buffer);
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页