【问题标题】:How to Minimise other windows while your application is running C#如何在应用程序运行 C# 时最小化其他窗口
【发布时间】:2017-09-11 19:35:43
【问题描述】:

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

【问题讨论】:

  • 虽然可能有一种技术方法可以做到这一点,但这对于应用程序来说似乎是一个非常糟糕的设计选择。您是否必须继续最小化在应用程序启动并仍在运行后打开/恢复的窗口?让一个应用程序将其意志强加于其他应用程序几乎总是一种糟糕的设计。
  • 这是一个要求,只要运行就需要限制用户使用
  • 这是一个不同的问题。你是说你必须防止人们在你的程序运行时对其他应用程序或其他应用程序进行 alt-tabbing 吗?你要找的是Kiosk mode
  • 使用键盘钩子已经完成了,我需要最小化用户可能在第二台显示器上运行的其他应用程序。

标签: c#


【解决方案1】:

使用下面的类并调用方法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());
        }
    } 
}

【讨论】:

    猜你喜欢
    • 2016-09-12
    • 1970-01-01
    • 2011-02-14
    • 2018-08-24
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多