using System; using System.Linq; using System.Text.RegularExpressions; using VDash.Utilities; namespace VDash.WindowSelectors { public class FromWindowTitle : IWindowSelector { WindowInfo m_Window = null; public bool RefreshOnGet { get; set; } = false; public string TitleFilter { get; set; } = ""; public TitleSearchType SearchType { get; set; } = TitleSearchType.Contains; public WindowInfo Window { get { if (RefreshOnGet) { m_Window = GetWindowUsingFilter(); return m_Window; } else { if (m_Window == null) { m_Window = GetWindowUsingFilter(); } return m_Window; } } } public void ForceWindowRefresh() { m_Window = GetWindowUsingFilter(); } public FromWindowTitle(string windowFilter, TitleSearchType searchType) { TitleFilter = windowFilter; SearchType = searchType; } public FromWindowTitle() { } internal WindowInfo GetWindowUsingFilter() { var windows = WindowInfo.GetWindowList(); if (SearchType == TitleSearchType.Contains) { var results = windows.Where(x => x.Title.ToLower().Contains(TitleFilter.ToLower())); if(results.Count() == 0) { throw new WindowNotFoundException("No windows match the filter settings."); } return results.First(); } if(SearchType == TitleSearchType.Regex) { var results = windows.Where(x => Regex.IsMatch(x.Title, TitleFilter)); if (results.Count() == 0) { throw new WindowNotFoundException("No windows match the filter settings."); } return results.First(); } throw new WindowNotFoundException("Error fetching matching window."); } public IntPtr GetWindowHandle() { return Window.hWnd; } public WindowInfo GetWindowInfo() { return Window; } public bool WindowExists() { //try //{ // var window = Window; //} //catch (WindowNotFoundException) //{ // return false; //} //return true; return User32.IsWindow(GetWindowHandle()); } public enum TitleSearchType { Contains = 0, Regex = 1 } } }