dinsdag 14 december 2010

C# - just 1 PInvoke to see if some window is fullscreen

Here's a quick summary:
1 Find the process (Process.Get...)
2 Get the process window area (PInvoke GetWindowRect)
3 Get the screen the process window is on (Screen.FromHandle)
4 Assert if the screen working area equals the process window area

But beware, toolbars and the taskbar affect the screen working area. The screen working area equals the screen bounds if no toolbars are present and the taskbar is on autohide. To see if the taskbar is on autohide or toggle autohide use the Windows 7 Code Pack.

And here's a short example:
            if (process != null && !process.HasExited)
            {
                RECT rect = new RECT();
                if (GetWindowRect(new HandleRef(process, process.MainWindowHandle), out rect))
                {
                    windowArea = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                    screenArea = Screen.FromHandle(process.MainWindowHandle).WorkingArea;
                    result = windowArea.Equals(screenArea);
                    logger.Trace("Window area [" + windowArea + "] " + (result ? "equals" : "does NOT equal") + " screen area [" + screenArea + "]");
                }
                else
                {
                    logger.DebugException("Failed to obtain window area for process with exception",
                        new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()));
                }
            }

Geen opmerkingen:

Een reactie posten