Esempio n. 1
0
        public async Task <string> GetTextAsync()
        {
            await OpenClipboard();

            try
            {
                IntPtr hText = UnmanagedMethods.GetClipboardData(UnmanagedMethods.ClipboardFormat.CF_UNICODETEXT);
                if (hText == IntPtr.Zero)
                {
                    return(null);
                }

                var pText = UnmanagedMethods.GlobalLock(hText);
                if (pText == IntPtr.Zero)
                {
                    return(null);
                }

                var rv = Marshal.PtrToStringUni(pText);
                UnmanagedMethods.GlobalUnlock(hText);
                return(rv);
            }
            finally
            {
                UnmanagedMethods.CloseClipboard();
            }
        }
Esempio n. 2
0
        private async Task <IDisposable> OpenClipboard()
        {
            while (!UnmanagedMethods.OpenClipboard(IntPtr.Zero))
            {
                await Task.Delay(100);
            }

            return(Disposable.Create(() => UnmanagedMethods.CloseClipboard()));
        }
Esempio n. 3
0
        public async Task ClearAsync()
        {
            await OpenClipboard();

            try
            {
                UnmanagedMethods.EmptyClipboard();
            }
            finally
            {
                UnmanagedMethods.CloseClipboard();
            }
        }
Esempio n. 4
0
        private async Task <IDisposable> OpenClipboard()
        {
            var i = OleRetryCount;

            while (!UnmanagedMethods.OpenClipboard(IntPtr.Zero))
            {
                if (--i == 0)
                {
                    throw new TimeoutException("Timeout opening clipboard.");
                }
                await Task.Delay(100);
            }

            return(Disposable.Create(() => UnmanagedMethods.CloseClipboard()));
        }
Esempio n. 5
0
        public async Task SetTextAsync(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            await OpenClipboard();

            try
            {
                var hGlobal = Marshal.StringToHGlobalUni(text);
                UnmanagedMethods.SetClipboardData(UnmanagedMethods.ClipboardFormat.CF_UNICODETEXT, hGlobal);
            }
            finally
            {
                UnmanagedMethods.CloseClipboard();
            }
        }