/* Loop method */ private void ListenLoop() { while (listening) { // Copy current foreground window title and window rect String previousForegroundWindowTitle = CurrentForegroundWindowTitle; Rectangle previousForegroundWindowRect = CurrentForegroundWindowRect; // Retrieve window handle IntPtr handle = GetForegroundWindowHandle(); // Update current foreground window title and window rect CurrentForegroundWindowTitle = Window.GetWindowTitleFromHandle(handle); CurrentForegroundWindowRect = Window.GetWindowRectFromHandle(handle); CurrentForegroundWindowHandle = handle; // Title Different? if (CurrentForegroundWindowTitle != String.Empty && CurrentForegroundWindowTitle != previousForegroundWindowTitle) { // Notify handler handler.NewForegroundWindow(CurrentForegroundWindowTitle, CurrentForegroundWindowRect, handle); // Check if the window actually exists currentWindowExists = Window.Exists(CurrentForegroundWindowTitle); } // Rectangle different? if (!CurrentForegroundWindowRect.Equals(previousForegroundWindowRect)) { // Notify handler.ForegroundWindowPositionChanged(CurrentForegroundWindowTitle, CurrentForegroundWindowRect); } // Check the windows that have been specifically added to our list, one by one CheckWindowsMonitorList(); // Wait Thread.Sleep(ListenInterval); } }
private void CheckWindowsMonitorList() { // Check each of the windows that we are monitoring int items = windowsListToMonitor.Count; for (int i = 0; i < items; i++) { Window window = windowsListToMonitor[i]; // (!window.Minimized && !window.Visible) is currently there for fixing a glitch in the SWC // client, which does not destroy the windows it creates but simply sets them invisible if (!window.Exists() || (!window.Minimized && !window.Visible)) { // Notify handler handler.WindowClosed(window.Title); // Add it to our remove list removeWindowList.Add(window); } else { // Still exist... is it resized? if (window.Minimized) { // Window is minimized... allow for maximize notifications to be sent again windowMaximizeNotificationSent[window] = false; // Did we already notify the handler? if (!HasMinimizeNotificationBeenSent(window)) { // Nop, notify handler.WindowMinimized(window.Title); // Set notification sent windowMinimizeNotificationSent[window] = true; } } else if (window.HasBeenMinimized) { // Window is visible... allow for minimize notifications to be sent again windowMinimizeNotificationSent[window] = false; // Did we already notify the handler? if (!HasMaximizeNotificationBeenSent(window)) { // Nop, notify handler.WindowMaximized(window.Title); // Set notification sent windowMaximizeNotificationSent[window] = true; } } } } // Actually remove the windows foreach (Window w in removeWindowList) { windowsListToMonitor.Remove(w); } // Cleanup removeWindowList.Clear(); }