Minimizing all open windows in C#

PInvoke.net is your friend :-)

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 {
class Program {
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
    static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

    const int WM_COMMAND = 0x111;
    const int MIN_ALL = 419;
    const int MIN_ALL_UNDO = 416;

    static void Main(string[] args) {
        IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); 
        System.Threading.Thread.Sleep(2000);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
    }
}
}

The site www.pinvoke.net has a lot of the information you require. For instance, this article on SendMessage and FindWindow:

http://www.pinvoke.net/default.aspx/user32.SendMessage http://www.pinvoke.net/default.aspx/user32.FindWindow

It's rather technical - of course - but basically you use p/invoke to call on the FindWindow and SendMessage API functions to accomplish what you want. =)


I've previously blogged on how to minimize & maximize using P/Invoke from C#: http://improve.dk/minimizing-and-maximizing-windows/


Not exactly the easiest way, but the manual way is to call the C++ implementation. http://pinvoke.net helps:

findwindow search results: http://pinvoke.net/search.aspx?search=findwindow&namespace=[All]

approximately the fourth result down helps in your case.