有没有办法检查另一个程序是否全屏运行

Que*_*mer 7 c# fullscreen

就像问题所说的那样.我可以看看其他人,程序是否全屏运行?

全屏意味着整个屏幕被遮挡,可能以与桌面不同的视频模式运行.

Sim*_*ier 12

这是一些代码.您需要关注多屏幕情况,尤其是Powerpoint等应用程序

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

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

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

    public static bool IsForegroundFullScreen()
    {
        return IsForegroundFullScreen(null);
    }

    public static bool IsForegroundFullScreen(Screen screen)
    {
        if (screen == null)
        {
            screen = Screen.PrimaryScreen;
        }
        RECT rect = new RECT();
        GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
        return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top).Contains(screen.Bounds); 
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您隐藏任务栏,这将错误地报告全屏应用程序 (2认同)

ARN*_*ARN 6

我做了一些修改.使用下面的代码,当隐藏任务栏或在第二个屏幕上时,它不会错误地返回true.在Win 7下测试.

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll", SetLastError = true)]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

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

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

    public static bool IsForegroundFullScreen()
    {
        return IsForegroundFullScreen(null);
    }


    public static bool IsForegroundFullScreen(System.Windows.Forms.Screen screen)
    {

        if (screen == null)
        {
            screen = System.Windows.Forms.Screen.PrimaryScreen;
        }
        RECT rect = new RECT();
        IntPtr hWnd = (IntPtr)GetForegroundWindow();


        GetWindowRect(new HandleRef(null, hWnd), ref rect);

        /* in case you want the process name:
        uint procId = 0;
        GetWindowThreadProcessId(hWnd, out procId);
        var proc = System.Diagnostics.Process.GetProcessById((int)procId);
        Console.WriteLine(proc.ProcessName);
        */


        if (screen.Bounds.Width == (rect.right - rect.left) && screen.Bounds.Height == (rect.bottom - rect.top))
        {
            Console.WriteLine("Fullscreen!")
            return true;
        }
        else {
            Console.WriteLine("Nope, :-(");
            return false;
        }


    }
Run Code Online (Sandbox Code Playgroud)

  • 这样做的原因是因为这里的屏幕尺寸必须与窗口完全匹配,而这仅适用于真正的全屏窗口。隐藏任务栏的最大化窗口仍然会有阴影,这会增加超出屏幕尺寸的范围并导致检查正确失败。如果禁用阴影(这是一个高级隐藏选项),那么此方法也会导致误报。 (3认同)