public WindowTitleWatcher(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { throw new ArgumentException($"{nameof(hWnd)} is null", nameof(hWnd)); } this.hWnd = hWnd; this.CurrentTitle = NativeWindows.GetWindowTitle(this.hWnd); }
private void CheckTimer_Elapsed(object sender, ElapsedEventArgs e) { lock (this.currentTitleLock) { string oldTitle = this.CurrentTitle; string newTitle = NativeWindows.GetWindowTitle(this.hWnd); if (!string.Equals(oldTitle, newTitle, StringComparison.CurrentCulture)) { this.CurrentTitle = newTitle; this.OnTitleChanged(oldTitle, newTitle); } } }
public static IntPtr GetMainWindowHandle(uint pid) { if (pid == 0) { return(IntPtr.Zero); } List <IntPtr> windows = NativeWindows.GetProcessWindows(pid); List <IntPtr> possibleMainWindows = windows.Where(h => { string className = NativeWindows.GetClassName(h); string windowName = NativeWindows.GetWindowTitle(h); return(!string.IsNullOrWhiteSpace(windowName) && spotifyMainWindowNames.Contains(className)); }).ToList(); if (possibleMainWindows.Count > 1) { IEnumerable <string> classNames = possibleMainWindows.Select(h => $"\"{NativeWindows.GetClassName(h)}\""); logger.Warn($"More than one ({possibleMainWindows.Count}) possible main windows located for Spotify: {string.Join(", ", classNames)}"); } return(possibleMainWindows.FirstOrDefault()); }