如何确定Windows 10应用商店应用是否最小化(标志性)

时间:2016-08-19 15:18:56

标签: c# dllimport windows-10-universal

我的桌面应用程序需要始终确定在使用x64 Win10的PC上运行的UWP应用程序的Windows状态(已恢复最小化)。当只有一个应用程序正在运行时,对本机方法 IsIconic (IntPtr hWnd)的调用适用于与 MainWindowTitle 相关联的 ProcessName “ApplicationFrameHost” UWP应用程序。但是,如果有2个UWP应用程序在运行,则只能确定首次打开的应用程序。

为了演示,下面的示例代码使用Windows 10天气和计算器应用程序为3个示例案例输出以下内容。

  

一个。仅运行1个UWP应用程序的情况(最小化)

Weather has 2 processes
Weather process name: ApplicationFrameHost
Weather is minimized: True
  

B中。计算器案件开1号,天气2号(两者实际上最小化)

Calculator has 2 processes
Calculator process name: ApplicationFrameHost
Calculator is minimized: True
Weather has 1 processes
Weather process name: Microsoft.Msn.Weather
Weather is minimized: False
  

℃。天气案例开启第一,计算器第二(两者实际上最小化)

Calculator has 1 processes
Calculator process name: Calculator
Calculator is minimized: False
Weather has 2 processes
Weather process name: ApplicationFrameHost
Weather is minimized: True

总之,A.工作,B。只能正确检测计算器,而C只能正确检测天气。

此方法循环显示窗口名称为Calclulator或Weather的进程,如果IsIconic方法为true,则退出为true。

public static void Main()
{
    var result = IsWindowMinimizedIconic("Calculator");
    Debug.WriteLine("Calculator is minimized: " + result.ToString());

    result = IsWindowMinimizedIconic("Weather");
    Debug.WriteLine("Weather is minimized: " + result.ToString());
}

public static bool IsWindowMinimizedIconic(string windowTitle)
{
    Process[] processes = Process.GetProcesses().Where(p => p.MainWindowTitle.StartsWith(windowTitle)).ToArray();
    Debug.WriteLine("{0} has {1} processes", windowTitle, processes.Length);

    foreach (Process process in processes)
    {
        Debug.WriteLine("{0} process name: {1}", windowTitle, process.ProcessName); 
        if (IsImmersiveProcess(process.Handle)) //TEST WINDOWS STORE APPS
        {
            // see if minimized/iconic
            if (IsIconic(process.MainWindowHandle))
            {
                return true;
            }
        }
    }
    return false;
}

/// <summary>
/// Determines if the specified window is minimized (iconic).
/// </summary>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);

/// <summary>
/// Determines if the specified window is UWP.
/// </summary>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsImmersiveProcess(IntPtr hWnd);

这似乎关键在于理解 ApplicationFrameHost 以及它与真实应用程序进程的关联。如何一致地获得窗口状态?

0 个答案:

没有答案