/// <summary> /// this application uses polling to serialise the call to this method /// </summary> /// <param name="windowTitleText"></param> /// <param name="className"></param> /// <returns></returns> public static IntPtr GetWindowHandle(Regex windowTitleText = null, Regex className = null, Regex process = null) { _windowHandle = IntPtr.Zero; var finder = new WindowFinder(); finder.FindWindows(0, className, windowTitleText, process, FoundWindow); int waitTimePerCycle = 100; int millSecondInFiveSeconds = 5 * 1000; //varriable polling for (int i = 0; i < millSecondInFiveSeconds / waitTimePerCycle; i++) { if (IntPtr.Zero == _windowHandle) { Thread.Sleep(100); } else { var result = _windowHandle; _windowHandle = IntPtr.Zero; return(result); } } throw new Exception("Window not found exception"); }
static void Main(string[] args) { // Introduced in the "Finding specific windows" blog, we use the WindowFinder class to find all Internet Explorer main window instances. WindowFinder wf = new WindowFinder(); // Find all visual studio instances //var appDimensions = GetApplicationWidthAndHeight("Paint"); //Console.WriteLine("Width: " + appDimensions.Item1 + ", Height: " + appDimensions.Item2); Console.Read(); }
/// <summary> /// returns application width as item 1, returns application height as item 2 /// This method uses polling to make the call synchronous /// </summary> /// <returns></returns> public static Tuple <int, int> GetApplicationWidthAndHeight(IntPtr handle) { _widthHeight = null; WindowFinder wf = new WindowFinder(); FoundWindow((int)handle); int waitTimePerCycle = 100; int millSecondInFiveSeconds = 5 * 1000; for (int i = 0; i < millSecondInFiveSeconds / waitTimePerCycle; i++) { if (null == _widthHeight) { Thread.Sleep(100); } else { return(_widthHeight); } } throw new TimeoutException("Timed out in GetApplicationWidthAndHeight"); }