private IntPtr OnClipboardUpdate(IntPtr hWnd, uint messageId, IntPtr wParam, IntPtr lParam)
        {
            if (messageId == (int)GlobalHook.MessageType.WM_CLIPBOARDUPDATE)
            {
                ClipboardUpdated?.Invoke(hWnd, messageId, wParam, lParam);
            }

            return(NativeClipboard.DefWindowProc(hWnd, messageId, wParam, lParam));
        }
        private void Dispose(bool disposing)
        {
            if (isDisposed || !disposing)
            {
                return;
            }

            isDisposed = true;
            NativeClipboard.RemoveClipboardFormatListener(WindowHandle);
            NativeClipboard.DestroyWindow(WindowHandle);
            internalWindowMessageHandler = null;
        }
        public ClipboardWindowMessageSink()
        {
            var className = $"sink@{Guid.NewGuid()}";

            internalWindowMessageHandler = OnClipboardUpdate;

            INativeClipboard.WindowClass windowClass;

            windowClass.style         = 0;
            windowClass.lpfnWndProc   = internalWindowMessageHandler;
            windowClass.cbClsExtra    = 0;
            windowClass.cbWndExtra    = 0;
            windowClass.hInstance     = IntPtr.Zero;
            windowClass.hIcon         = IntPtr.Zero;
            windowClass.hCursor       = IntPtr.Zero;
            windowClass.hbrBackground = IntPtr.Zero;
            windowClass.lpszMenuName  = "";
            windowClass.lpszClassName = className;


            NativeClipboard.RegisterClass(ref windowClass);

            // Creates window to register clipboard update messages
            WindowHandle = NativeClipboard.CreateWindowEx(0,
                                                          className,
                                                          "",
                                                          0,
                                                          0, 0,
                                                          1, 1,
                                                          IntPtr.Zero,
                                                          IntPtr.Zero,
                                                          IntPtr.Zero,
                                                          IntPtr.Zero);

            NativeClipboard.AddClipboardFormatListener(WindowHandle);
        }