I need to minimize all other windows while my application is running. Is there a better way to do it using APIs?
Asked
Active
Viewed 668 times
-1
-
While there probably is a technical way to do this, this seems like a monumentally bad design choice for an application. Would you have to keep minimizing windows that was opened/restored after your application has started and is still running? Having one application just imposing its will on *other* applications is almost always a bad design. – Lasse V. Karlsen Sep 11 '17 at 11:45
-
This is a requirement, needs to restrict users to it as long as it runs – Chikku Jacob Sep 11 '17 at 12:19
-
That is a different question. Are you saying you have to prevent people from alt-tabbing to other applications or whatnot while your program is running? What you're looking for is [Kiosk mode](https://learn.microsoft.com/en-us/windows/configuration/set-up-a-kiosk-for-windows-10-for-desktop-editions). – Lasse V. Karlsen Sep 11 '17 at 12:21
-
That's already done using keyboard hook, I need to minimize other apps which the user might be running on second monitor. – Chikku Jacob Sep 12 '17 at 05:34
-
Related post - [Minimizing all open windows in C#](https://stackoverflow.com/q/785054/465053) – RBT Jan 13 '22 at 11:56
1 Answers
0
Use the following class and Call the method MinimizeAll();
to do it.
Try this code:
using System;
using System.Runtime.InteropServices;
public class WindowMinimize
{
const int SW_SHOWMINNOACTIVE = 7;
// Hide other windows
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void MinimizeWindow(IntPtr handle)
{
ShowWindow(handle, SW_SHOWMINNOACTIVE);
}
public static void MinimizeAll()
{
System.Diagnostics.Process thisProcess =
System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] processes =
System.Diagnostics.Process.GetProcesses();
try
{
foreach (System.Diagnostics.Process process in processes)
{
if (process == thisProcess) continue;
System.IntPtr handle = process.MainWindowHandle;
if (handle == System.IntPtr.Zero) continue;
MinimizeWindow(handle);
} //loop
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
}

Rob Quincey
- 2,834
- 2
- 38
- 54

Chikku Jacob
- 2,114
- 1
- 18
- 33