public FocusInfo GetCurrentFocus() { try { IntPtr handle = WinApi.GetForegroundWindow(); FocusInfo focusInfo = new FocusInfo() { HWND = (long)handle, }; var pid = WinApi.GetWindowProcessId(handle); focusInfo.PID = pid; var process = Process.GetProcessById((int)pid); focusInfo.WindowTitle = WinApi.GetWindowText(handle); focusInfo.ExecutableFile = process.MainModule.FileName; focusInfo.ExecutableName = process.MainModule.ModuleName; return(focusInfo); } catch (Exception ex) { return(new FocusInfo() { Error = ex.ToString() }); } }
private void UpdateCurrentApplication() { updateCounter++; var currentFocus = GetCurrentFocus(); if (FocusInfo.Equality.Equals(currentFocus, LastFocus)) { return; } textBox2.Text = $"{currentFocus.WindowTitle}, ({updateCounter})"; LastFocus = currentFocus; RegisterFocusChange(new FocusChange { Timestamp = DateTime.Now.ToString(FocusChange.DateTimeFormat), FocusInfo = currentFocus, }); }