如何在应用程序运行时最小化其他窗口C#

时间:2017-09-11 11:00:20

标签: c#

我需要在应用程序运行时最小化所有其他窗口。有没有更好的方法来使用API​​?

1 个答案:

答案 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());
        }
    } 
}