예제 #1
0
        static Exception GenerateInstallFailureException()
        {
            var errorCode = Marshal.GetLastWin32Error();

            var existingOwner = ClipboardApi.GetClipboardOwner();
            var ownerTitle    = WindowApi.GetWindowTitle(existingOwner);

            return(new InvalidOperationException($"Could not install a clipboard hook for the main window. The window '{ownerTitle}' currently owns the clipboard. The last error code was {errorCode}."));
        }
예제 #2
0
        public IDataSource GetDataSource()
        {
            var activeWindowHandle = WindowApi.GetForegroundWindow();

            var windowTitle = WindowApi.GetWindowTitle(activeWindowHandle);
            var windowIcon  = GetWindowIcon(activeWindowHandle);

            var iconBytes = imagePersistenceService.ConvertBitmapSourceToByteArray(windowIcon);

            return(new DataSource(iconBytes, windowTitle));
        }
        public void Start()
        {
            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = _cancellationTokenSource.Token;

            Task listener = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    IntPtr newFocusedWindowHandle = WindowApi.GetForegroundWindow();
                    string newFocusedWindowTitle  = WindowApi.GetWindowTitle(newFocusedWindowHandle);

                    //Only trigger event & update when focused window title is different
                    if (newFocusedWindowTitle != null && newFocusedWindowTitle != FocusedWindowInfo?.Title)
                    {
                        int threadId = WindowApi.GetWindowProcessId(newFocusedWindowHandle, out int processId);
                        WindowInfo newFocusedWindowInfo = new WindowInfo()
                        {
                            Title     = newFocusedWindowTitle,
                            Handle    = newFocusedWindowHandle,
                            ProcessId = processId,
                            ThreadId  = threadId
                        };
                        FocusedWindowTitleChanged?.Invoke(this, new FocusedWindowTitleChangedEventArgs()
                        {
                            OldFocusedWindow = FocusedWindowInfo,
                            NewFocusedWindow = newFocusedWindowInfo
                        });
                        FocusedWindowInfo = newFocusedWindowInfo;
                    }

                    Thread.Sleep(PollDelayMilliseconds);
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }