Пример #1
0
        public static Process FindSpotifyProcess()
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug("Looking for Jirafy process...");
            }

            List <Process> spotifyProcesses  = Process.GetProcessesByName(ProcessName).ToList();
            List <Process> windowedProcesses = spotifyProcesses.Where(p => p.MainWindowHandle != IntPtr.Zero).ToList();

            if (windowedProcesses.Count > 1)
            {
                IEnumerable <string> classNames = windowedProcesses.Select(p => $"\"{NativeWindows.GetClassName(p.MainWindowHandle)}\"");
                logger.Warn($"More than one ({windowedProcesses.Count}) \"{ProcessName}\" process has a non-null main window: {string.Join(", ", classNames)}");
            }

            Process process = windowedProcesses.FirstOrDefault();

            // If none of the Spotify processes found has a valid MainWindowHandle,
            // then Spotify has probably been minimized to the tray: we need to check every window.
            if (process == null)
            {
                foreach (Process p in spotifyProcesses)
                {
                    if (IsMainSpotifyProcess((uint)p.Id))
                    {
                        return(p);
                    }
                }
            }

            return(process);
        }
Пример #2
0
        public static bool IsMainSpotifyProcess(uint pid)
        {
            List <IntPtr> windows = NativeWindows.GetProcessWindows(pid);
            IntPtr        hWnd    = windows.FirstOrDefault(h => spotifyMainWindowNames.Contains(NativeWindows.GetClassName(h)));

            return(hWnd != IntPtr.Zero);
        }
Пример #3
0
        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);
        }
Пример #4
0
 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);
         }
     }
 }
Пример #5
0
        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());
        }