Minimizing All Open Windows

Minimizing All Open Windows

It is quite easy to minimize all currently opened windows with C#. All you need to know is a few APIs:


Here is a small example:

public partial class Form1 : Form{public Form1(){InitializeComponent();}[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]static extern IntPtr SendMessageTimeout(IntPtr windowHandle,uint Msg,IntPtr wParam,IntPtr lParam,SendMessageTimeoutFlags flags,uint timeout,out IntPtr result);[Flags]enum SendMessageTimeoutFlags : uint{SMTO_NORMAL = 0x0,SMTO_BLOCK = 0x1,SMTO_ABORTIFHUNG = 0x2,SMTO_NOTIMEOUTIFNOTHUNG = 0x8}const int WM_COMMAND = 0x111;const int MIN_ALL = 419;const int MIN_ALL_UNDO = 416;private void button1_Click(object sender, EventArgs e){IntPtr OutResult;IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);System.Threading.Thread.Sleep(2000);SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);}}

It makes use of the above-mentioned APIs to send the MIN_ALL commands so that all windows become minimized, similar to what the ‘Show Desktop’ button on the TaskBar does.


Share the Post: