Esempio n. 1
0
        /// <summary>
        /// Initializes <see cref="ClipboardManager"/> and places the <see cref="MainForm"/> window in the system-maintained clipboard format listener list.
        /// </summary>
        /// <param name="window">A <see cref="MainForm"/> object.</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidOperationException"/>
        /// <exception cref="NotSupportedException"/>
        internal static void Initialize(MainForm window)
        {
            if (window == null)
            {
                throw new ArgumentNullException(nameof(window));
            }
            else if (history != null)
            {
                throw new InvalidOperationException(nameof(ClipboardManager) + " is already initialized.");
            }

            if (Win32.ShouldUseWin32())
            {
                try
                {
                    hwnd = window.Handle;
                    if (!UnsafeNativeMethods.AddClipboardFormatListener(hwnd))
                    {
                        hwnd = IntPtr.Zero;
                        var ex = new Win32Exception(Marshal.GetLastWin32Error());
                        throw new NotSupportedException(ex.Message, ex);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    hwnd = IntPtr.Zero;
                }
            }

            history = new FixedSizeQueue <ClipboardTextData>(Globals.Settings.ClipboardHistorySize);

            try
            {
                var dataObject = Clipboard.GetDataObject();
                if (ClipboardTextData.IsTextFormat(dataObject))
                {
                    history.Enqueue(new ClipboardTextData(dataObject));
                }
            }
            catch (ExternalException) { }
            catch (ThreadStateException) { }
        }
Esempio n. 2
0
        /// <summary>
        /// Handles <see cref="Form.WndProc"/> for a clipboard update message.
        /// Returns <see langword="true"/> if new clipboard data is added; <see langword="false"/> otherwise.
        /// </summary>
        /// <param name="m">A <see cref="Message"/> object.</param>
        /// <exception cref="InvalidOperationException"/>
        /// <exception cref="ThreadStateException"/>
        internal static bool HandleWndProc(ref Message m)
        {
            if (m.Msg == UnsafeNativeMethods.WM_CLIPBOARDUPDATE)
            {
                try
                {
                    var dataObject = Clipboard.GetDataObject();
                    if (ClipboardTextData.IsTextFormat(dataObject))
                    {
                        history.Enqueue(new ClipboardTextData(dataObject));
                        return(true);
                    }
                }
                catch (NullReferenceException ex)
                {
                    throw new InvalidOperationException(nameof(ClipboardManager) + " is either not initialized or disposed.", ex);
                }
                catch (ExternalException) { }
                //catch (ThreadStateException) { }
            }

            return(false);
        }