我需要在应用程序运行时最小化所有其他窗口。有没有更好的方法来使用API?
答案 0 :(得分:0)
使用以下类并调用方法MinimizeAll();
来执行此操作。
试试这段代码:
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());
}
}
}