// 刷新列表 private void refresh() { listView.Items.Clear(); var allWindows = TopLevelWindowUtils.FindWindows(w => w.IsValid && w.GetWindowText() != "" && w.GetWindowText() != "Default IME" && w.GetWindowText() != "MSCTFIME UI" ); foreach (var windowHandle in allWindows) { try { listView.Items.Add(new WindowStatus(windowHandle)); } catch (Exception e) { Console.WriteLine(e.Message); } } listView.Items.SortDescriptions.Clear(); listView.Items.SortDescriptions.Add(lastSortDescription); }
public static List <Taskbar> GetAllSecondaryTaskbars() { var allHandles = TopLevelWindowUtils.FindWindows(wh => wh.GetClassName().Contains(SecondaryClassName)).ToList(); var allSecondaryTaskbars = new List <Taskbar>(); allHandles.ForEach(x => allSecondaryTaskbars.Add(new Taskbar(x))); return(allSecondaryTaskbars); }
public void ListAllWindows() { var allWindows = TopLevelWindowUtils.FindWindows(w => w.GetWindowText() != ""); Console.WriteLine("All the windows that are currently present:"); foreach (var windowHandle in allWindows) { Console.WriteLine(windowHandle.GetWindowText()); } }
private static void Main(string[] args) { if (args.Length == 1) { switch (args[0]) { case "--force": force = true; break; case "--version": Console.Out.WriteLine($"Alice's Window Closer App v{version}"); Environment.Exit(0); //We close because we were only showing the version. break; default: Console.Out.WriteLine($"Alice's Window Closer App v{version.Major}.{version.Minor}"); Console.Out.WriteLine("--force Kill any processes that have an open window. (Dangerous!)"); Console.Out.WriteLine("--help Show help flags"); Environment.Exit(0); //We close because we were only showing the help. break; } } List <Process> taskBarProcesses = Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).Where(p => p.MainWindowHandle != GetConsoleWindow()).ToList(); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); foreach (InternetExplorer item in shellWindows) { try { item.Quit(); } catch (System.Runtime.InteropServices.COMException e) { Console.Error.WriteLine("Error trying to close " + item.FullName + ", Reason: " + e.Message); } } taskBarProcesses.ForEach((task) => { #if DEBUG if (task.ProcessName == "devenv") { return; } #endif TopLevelWindowUtils.FindWindows((wh) => IsWindowHandleOwnedBy(wh, task)).ToList() .ForEach((wh) => { Console.Out.WriteLine($"Closing {task.ProcessName}"); PostMessage(wh.RawPtr.ToInt32(), WM_Close, 0, 0); int LastError = Marshal.GetLastWin32Error(); string errorMessage = new Win32Exception(LastError).Message; if (LastError != 0) { Console.Error.WriteLine($"Failed to close {task.MainWindowTitle}: {errorMessage}"); } }); if (force) { Thread.Sleep(500); Console.Out.WriteLine($"Killing {task.ProcessName}"); try { task.Refresh(); if (!task.HasExited) { task.Kill(); } } catch (InvalidOperationException e) { Console.Error.WriteLine($"Something went wrong trying to kill {task.ProcessName}: {e.Message}"); } } }); #if DEBUG Console.ReadKey(); #endif }
static public IEnumerable <WindowHandle> GetAllVisibleWindows() { return(TopLevelWindowUtils.FindWindows(w => (w.IsVisible() == true) && (w.GetWindowText() != ""), FindWindowSortType.SortByWinHandle)); }
public static List <WindowHandle> GetWindows() { WindowHandle current = TopLevelWindowUtils.GetForegroundWindow(); return(TopLevelWindowUtils.FindWindows(wh => wh.IsValid && wh.IsVisible() && wh.RawPtr != current.RawPtr).ToList()); }