WIN32API获取剪切板文本

        [DllImport("kernel32.dll")]
        public static extern IntPtr GlobalSize(IntPtr handle);


        [DllImport("kernel32.dll")]
        static extern IntPtr GlobalLock(IntPtr hMem);


        [DllImport("user32.dll")]
        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()
        {


            OpenClipboard(IntPtr.Zero);
            //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);
            CloseClipboard();


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