How do I find out if the desktop is in the foreground or is the active window? RRS feed

  • Question

  •  What i want to find out is if the desktop is shown (i.e. in the foreground) and no other applications are visible (i.e. all minimized). I would think this is a very quick process using either GetDesktopWindow(), GetForegroundWindow() or GetTopWindow().
     Any way to do this easily in C# using Pinvoke or .NET libraries?

    Tuesday, April 21, 2009 8:14 AM

All replies

  • Hi Kwacks,

    Based on my understanding, you want to minimize all windows on the desktop and show desktop. Please have a try using the following sample code through Type.InvokeMember Method. Correct me if I misunderstand you.
            private Type typeShell = null;
            private object objShell = Type.Missing;
            // Invoke MinimizeAll to minimize all windows on the desktop 
            private void button1_Click(object sender, EventArgs e)
                // Get the type from Shell.Application
                typeShell = Type.GetTypeFromProgID("Shell.Application");
                // Create object objShell from the type typeShell
                objShell = Activator.CreateInstance(typeShell);
                // Invoke MinimizeAll to show the desktop
                typeShell.InvokeMember("MinimizeAll", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
            // Invoke UndoMinimizeAll to restore all the Desktop windows
            private void button2_Click(object sender, EventArgs e)
                typeShell = Type.GetTypeFromProgID("Shell.Application");
                objShell = Activator.CreateInstance(typeShell);
                typeShell.InvokeMember("UndoMinimizeAll", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);

    Best regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Guo Surfer Monday, April 27, 2009 5:42 AM
    • Marked as answer by Guo Surfer Tuesday, April 28, 2009 3:38 AM
    • Unmarked as answer by Kwacks Tuesday, April 28, 2009 4:22 AM
    • Unproposed as answer by Kwacks Tuesday, April 28, 2009 4:23 AM
    Thursday, April 23, 2009 7:21 AM