using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace MinimizeAll { public partial class Form1 : Form { [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount); // This static method is required because legacy OSes do not support // GetWindowLongPtr public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex) { if (IntPtr.Size == 8) return GetWindowLongPtr64(hWnd, nIndex); else return new IntPtr(GetWindowLong32(hWnd, nIndex)); } [DllImport("user32.dll", EntryPoint = "GetWindowLong")] private static extern int GetWindowLong32(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")] private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex); public delegate bool EnumDelegate(IntPtr hWnd, int i); [DllImport("user32.dll")] static extern bool CloseWindow(IntPtr hWnd); [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { EnumDesktopWindows(IntPtr.Zero,delegate(IntPtr win,int i) { //filter the windows: if ((GetWindowLongPtr(win, -20).ToInt32() & 0x00000080) > 0) { //tool window } else if (!IsWindowVisible(win)) { //window not visible } else { //maybe.... StringBuilder sb = new StringBuilder(); GetWindowText(win, sb, 100); if (sb.Length > 0) { CloseWindow(win); } } return true; }, IntPtr.Zero); } } }