public void BringToFront() { var wa = new WindowApi(); var window = wa.WindowFromPoint(new Point(1000, 500)); wa.BringToFront(window); var frontWindow = wa.GetForegroundWindow(); Expect(window.Ptr, Is.EqualTo(frontWindow.Ptr)); }
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); }