private void OnObjectCreate(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { int pid; GetWindowThreadProcessId(hWnd, out pid); // return value is thread id i.e. not useful here if (Process.GetProcessById(pid).ProcessName == "spotify") { _objectCreateHook.Stop(); _objectCreateHook = null; HookSpotify(); } }
private void HookSpotify() { try { _spotify = Process.GetProcessesByName("spotify")[0]; } catch (IndexOutOfRangeException) { // spotify isn't running; hook ObjectCreate and wait for it to launch _objectCreateHook = new WinEventHook(OnObjectCreate, WinEventHook.EventConstant.EVENT_OBJECT_CREATE); return; } // in case Mutify has been launched while Spotify is playing something if (_currentTitle != null) { VolumeControl.SetApplicationMute(_spotify.Id, _blacklist.Contains(_currentTitle)); MuteAdButton.IsEnabled = true; } _spotify.EnableRaisingEvents = true; // register a handler _spotify.Exited += SpotifyExited; // for spotify's exit // hook changes to Spotify's main window title so titles can be checked against the blacklist _windowNameHook = new WinEventHook(OnWindowNameChange, WinEventHook.EventConstant.EVENT_OBJECT_NAMECHANGE, _spotify.Id); }
private void UnhookSpotify() { if (_windowNameHook != null) { _windowNameHook.Stop(); _windowNameHook = null; } }