コード例 #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) { }
        }
コード例 #2
0
            public NotificationForm()
            {
                UnsafeNativeMethods.SetParent(Handle, UnsafeNativeMethods.HWND_MESSAGE);

                UnsafeNativeMethods.AddClipboardFormatListener(Handle);
            }