如何检查应用程序是不是在任何屏幕上运行全屏模式?

Posted

技术标签:

【中文标题】如何检查应用程序是不是在任何屏幕上运行全屏模式?【英文标题】:How to check whether application is running fullscreen mode on any screen?如何检查应用程序是否在任何屏幕上运行全屏模式? 【发布时间】:2015-11-21 13:02:04 【问题描述】:

我想检查一下,是否有任何屏幕以全屏模式托管应用程序。我只有一个屏幕的解决方案,这是从这里复制的代码:[WPF] [C#] How-to : Detect if another application is running in full screen mode. 这个解决方案基于

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

仅收集 活动 窗口句柄。问题是,我有两个屏幕。我搜索了很多网站,但没有一个能回答我的问题。不是截屏,简单,不依赖P/Invoke。

这可能吗?

【问题讨论】:

GetForgroundWindow 只是给你一个窗口句柄(hWnd)。使用任何你喜欢的枚举窗口的方法——它们都会给你一个 hWnd——并插入这些值而不是前景 hWnd。 试试这个:codewrecks.com/blog/index.php/2014/01/29/… 通过使用绑定,您可以存储变量来检查屏幕是否最大化 @PieterSchool - 这与 WPF 无关,但感谢您的宝贵时间 :) @Fka 我认为是因为链接抱歉 @Fka:想象一张纸。绘制两个不同大小的矩形,每个矩形共享一个边框。由于缺乏更好的术语,该论文代表了您的虚拟桌面。矩形代表您的显示器。 GetWindowRectScreen.Bounds 在虚拟桌面坐标中给出。无需担心显示器的分辨率。 【参考方案1】:

这里没有现成的解决方案,但让我们看看..

获取所有显示窗口的列表并检查这些窗口的位置和大小 - 可能,有很多工具可以做到这一点,很多文章,我会跳过这一篇。然后,您可以为每个或某些窗口调用MonitorFromWindow,并将窗口尺寸和位置与监视器信息进行比较。如果 windowpos ~= 0,0 和 windowssize ~= monitorresolution 您可以假设此窗口处于全屏模式。

另一方面,如果已经有一个所有 HWND 的列表,那么为什么不只是 Query the window for its placement 并检查 WINDOWPLACEMENT.showCmd 的 SW_MAXIMIZE/SW_SHOWMAXIMIZED 标志。这不会告诉你它是哪台显示器,但至少应该告诉你窗口是否已最大化以及它是否足以满足你的需求..

我不知道这样做会多快/多慢,但是,是的,这似乎是可能的。

【讨论】:

【参考方案2】:

您可以将EnumWindowsScreen.FromHandle 结合使用。也许GetWindowRect() 用于计算。

类似(伪代码!):

//------------------------------
//this sample code is taken from http://pinvoke.net/default.aspx/user32/EnumWindows.html

public delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumWindows(EnumedWindow lpEnumFunc, ArrayList lParam);

public static ArrayList GetWindows()
    
    ArrayList windowHandles = new ArrayList();
    EnumedWindow callBackPtr = GetWindowHandle;
    EnumWindows(callBackPtr, windowHandles);
    return windowHandles;     


private static bool GetWindowHandle(IntPtr windowHandle, ArrayList windowHandles)

    windowHandles.Add(windowHandle);
    return true;


//------------------------------

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, [In,Out] ref Rect rect);

[StructLayout(LayoutKind.Sequential)]
private struct Rect

    public int Left;
    public int Top;
    public int Right;
    public int Bottom;


static void Main() 
    foreach(IntPtr handle in GetWindows())
    
      Screen scr = Screen.FromHandle(handle);

      if(IsFullscreen(handle, scr))
      
          // the window is fullscreen...
      
    


private bool IsFullscreen(IntPtr wndHandle, Screen screen)

    Rect r = new Rect();
    GetWindowRect(wndHandle, ref r);
    return new Rectangle(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top)
                          .Contains(screen.Bounds);

【讨论】:

【参考方案3】:

我写了一段正在运行的代码

namespace EnumWnd

using System;
using System.Runtime.InteropServices;
using System.Text;

[StructLayout(LayoutKind.Sequential)]
public struct Rect

    public int Left;

    public int Top;

    public int Right;

    public int Bottom;


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct MonitorInfoEx

    public int cbSize;
    public Rect rcMonitor;
    public Rect rcWork;
    public UInt32 dwFlags;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDeviceName;


internal class Program

    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    protected static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll")]
    protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

    [DllImport("user32.dll")]
    protected static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("User32")]
    public static extern IntPtr MonitorFromWindow(IntPtr hWnd, int dwFlags);

    [DllImport("user32", EntryPoint = "GetMonitorInfo", CharSet = CharSet.Auto,
        SetLastError = true)]
    internal static extern bool GetMonitorInfoEx(IntPtr hMonitor, ref MonitorInfoEx lpmi);

    protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
    
        const int MONITOR_DEFAULTTOPRIMARY = 1;
        var mi = new MonitorInfoEx();
        mi.cbSize = Marshal.SizeOf(mi);
        GetMonitorInfoEx(MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY), ref mi);

        Rect appBounds;
        GetWindowRect(hWnd, out appBounds);
        int size = GetWindowTextLength(hWnd);
        if (size++ > 0 && IsWindowVisible(hWnd))
        
            var sb = new StringBuilder(size);
            GetWindowText(hWnd, sb, size);

            if (sb.Length > 20)
            
                sb.Remove(20, sb.Length - 20);
            

            int windowHeight = appBounds.Right - appBounds.Left;
            int windowWidth = appBounds.Bottom - appBounds.Top;

            int monitorHeight = mi.rcMonitor.Right - mi.rcMonitor.Left;
            int monitorWidth = mi.rcMonitor.Bottom - mi.rcMonitor.Top;

            bool fullScreen = (windowHeight == monitorHeight) && (windowWidth == monitorWidth);

            sb.AppendFormat(" Wnd:(0 | 1) Mtr:(2 | 3 | Name: 4) - 5", windowWidth, windowHeight, monitorWidth, monitorHeight, mi.szDeviceName, fullScreen);

            Console.WriteLine(sb.ToString());
        
        return true;
    

    private static void Main()
    
        while (true)
        
            EnumWindows(EnumTheWindows, IntPtr.Zero);
            Console.ReadKey();
            Console.Clear();
        
    

    protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

感谢@SamAxe 和@quetzalcoatl 为我提供有用的提示。

【讨论】:

以上是关于如何检查应用程序是不是在任何屏幕上运行全屏模式?的主要内容,如果未能解决你的问题,请参考以下文章

Python检查应用程序是不是全屏

在 OS X 上防止屏幕变暗和显示睡眠

如何使用Javascript使窗口全屏(在屏幕上伸展)

检测浏览器是不是处于全屏模式

如何使用/获取 Apple Watch 全屏模式

如何强制 iOS 应用以纵向模式启动