12

Just like the question says. Can I see if someone else, program, is running full screen?

Full screen means that the entire screen is obscured, possibly running in a different video mode than the desktop.

QueueHammer
  • 10,515
  • 12
  • 67
  • 91
  • do you want to know if a program YOU have programmatic control over is full screened or do you want to find out if another program is running fullscreen? – Justin C Sep 19 '10 at 00:05
  • 2
    related: http://stackoverflow.com/questions/3536373/detect-if-user-has-any-application-running-in-fullscreen. Also http://stackoverflow.com/questions/3686311/my-c-winform-needs-to-detect-when-other-applications-enter-exit-run-in-true-full. I suspect the latter will answer your question. – Jim Mischel Sep 19 '10 at 01:56

2 Answers2

15

Here is some code that does it. You want to take care about the multi screen case, especially with applications like 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); 
    }
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 2
    If you hide the taskbar, this will falsely report a fullscreen app – Rolle Feb 04 '14 at 14:46
  • @Rolle is right. Any solutions for that? I guess it's difficult to separate a maximized Notepad running on a secondary screen (with no taskbar) from a fullscreen video/game/PowerPoint presentation without taking each process into account. – Erlend D. Jun 18 '15 at 06:40
  • Don't know exactly what you're after, but you can replace `screen.Bounds` by `screen.WorkingArea` if you want to take the taskbar in account. – Simon Mourier Jun 18 '15 at 08:15
  • 1
    As @Rolle wrote, this method will falsely report a fullscreen app if the taskbar is hidden (as it is on secondary screens on Windows 7 or older). I was just wondering if there is any way of separating a maximized notepad (which your method will mark as a fullscreen application if the taskbar is hidden) from "true" fullscreen applications like a PowerPoint presentation, a movie, or a game. But I'm fairly sure the answer is no -- there is no technical difference between such applications, only user perceived differences. – Erlend D. Jun 23 '15 at 11:08
  • @erl One possible solution would be to check the foreground window for the absence of a title bar. That's the only real difference between something like a PowerPoint slide show and Notepad maximized. – Cody Gray - on strike Feb 23 '16 at 16:23
11

I did some modifications. With the code below it doesn't falsely return true when taskbar is hidden or on a second screen. Tested under 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;
        }


    }
ARN
  • 679
  • 7
  • 15
  • 3
    The reason this works is because the screen size here has to match exactly to the window, and this is only the case for truly fullscreen windows. Maximized windows with taskbar hidden will still have a drop shadow which increased the bounds beyond the screen dimensions and causes the check to fail correctly. If drop shadows are disabled (which is an advanced hidden away option) then this method will cause false positives as well. – letmaik Nov 14 '20 at 12:19