using System; using System.Diagnostics; using System.Linq; using VDash.Utilities; namespace VDash.WindowSelectors { public class FromProcessName : IWindowSelector { internal string m_ProcName = ""; public string ProcessName { get { return m_ProcName; } set { m_ProcName = value; m_Processes = null; } } public FromProcessName(string procName) { if (string.IsNullOrWhiteSpace(procName)) { throw new ArgumentNullException("procName", "procName must not be null, whitespace, or empty."); } ProcessName = procName; } public FromProcessName() { } Process[] m_Processes = null; public Process[] GetProcesses() { if(m_Processes == null) { m_Processes = Process.GetProcessesByName(ProcessName); } return m_Processes; } public Process GetProcess() { var procs = GetProcesses(); if (procs.Count() <= 0) { throw new WindowNotFoundException($"No process found with name {ProcessName}."); } return procs.First(); } public IntPtr GetWindowHandle() { return GetProcess().MainWindowHandle; } public WindowInfo GetWindowInfo() { var ret = WindowInfo.GetWindowList().SingleOrDefault(x => x.hWnd == GetWindowHandle()); if(ret == null) { throw new WindowNotFoundException($"Unable to locate window for process {ProcessName}."); } return ret; } public bool WindowExists() { try { return User32.IsWindow(GetWindowHandle()); } catch { return false; } } } }