16

I saw this C++ code on a forum which minimizes all open windows

#define MIN_ALL        419
#define MIN_ALL_UNDO   416

int main(int argc, char* argv[])
{
    HWND lHwnd = FindWindow("Shell_TrayWnd",NULL);
    SendMessage(lHwnd,WM_COMMAND,MIN_ALL,0);
    Sleep(2000);
    SendMessage(lHwnd,WM_COMMAND,MIN_ALL_UNDO,0);
    return 0;
}

How can I access the FindWindow and SendMessage API function and the HWND type in C#.net?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
  • if it helps, you can just show the desktop (not effectively a minimize) using the shell32 class – nawfal Sep 19 '12 at 09:51
  • Related post - [How to Minimise other windows while your application is running C#](https://stackoverflow.com/q/46154112/465053) – RBT Jan 13 '22 at 11:55

4 Answers4

33

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);
    }
}
}
Stanislav Kniazev
  • 5,386
  • 3
  • 35
  • 44
  • 2
    what if I want to minimize all windows except for the app that I am running? – Hamish Grubijan Jan 25 '11 at 20:57
  • 1
    Hamish, if you post your question as a question, not a comment, I'm sure you'll get an answer pretty quick and that answer will be formatted better than this discussion in comments allows. – Stanislav Kniazev Jan 26 '11 at 12:33
5

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. =)

J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • When I try adding the import code to my existing code, Visual C# 2008 Express tells me that it expects either "class, delegate, interface or struct" instead of IntPtr. –  Apr 24 '09 at 09:12
3

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

Mark S. Rasmussen
  • 34,696
  • 4
  • 39
  • 58
1

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.

PatTech
  • 397
  • 2
  • 10