63 lines
1.6 KiB
C#
63 lines
1.6 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using VDash.Utilities;
|
|
|
|
namespace VDash.WindowSelectors
|
|
{
|
|
public class FromProcessName : IWindowSelector
|
|
{
|
|
string m_ProcName = "";
|
|
public FromProcessName(string procName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(procName))
|
|
{
|
|
throw new ArgumentNullException("procName", "procName must not be null, whitespace, or empty.");
|
|
}
|
|
|
|
m_ProcName = procName;
|
|
}
|
|
|
|
Process[] processes = null;
|
|
public Process[] GetProcesses()
|
|
{
|
|
if(processes == null)
|
|
{
|
|
processes = Process.GetProcessesByName(m_ProcName);
|
|
}
|
|
|
|
return processes;
|
|
}
|
|
|
|
public Process GetProcess()
|
|
{
|
|
var procs = GetProcesses();
|
|
|
|
if (procs.Count() <= 0)
|
|
{
|
|
throw new WindowNotFoundException($"No process found with name {m_ProcName}.");
|
|
}
|
|
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 {m_ProcName}.");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public bool WindowExists()
|
|
{
|
|
return User32.IsWindow(GetWindowHandle());
|
|
}
|
|
}
|
|
}
|