Reputation: 1040
I need to detect if some application is currently running in full screen mode. If yes, then I must stop my application. So, how can I detect that? p.s. Win32 C++
Upvotes: 18
Views: 10725
Reputation: 4074
All other answers are rather hackish.
Windows Vista, Windows 7 and up support SHQueryUserNotificationState()
:
QUERY_USER_NOTIFICATION_STATE pquns;
SHQueryUserNotificationState(&pquns);
From this "notification state" it is possible to infer the fullscreen state. Basically, when there is an an app running in fullscreen mode, Windows reports a "busy" notification state.
QUNS_NOT_PRESENT
- not fullscreen (machine locked/screensaver/user switching)QUNS_BUSY
– fullscreen (the F11 fullscreen, also all video games I tried use this)QUNS_RUNNING_D3D_FULL_SCREEN
– fullscreen (Direct3D application is running in exclusive mode, i.e. fullscreen)QUNS_PRESENTATION_MODE
– fullscreen (a special mode for showing presentations, which are fullscreen)QUNS_ACCEPTS_NOTIFICATIONS
– not fullscreenQUNS_QUIET_TIME
– not fullscreenQUNS_APP
– probably fullscreen (not sure: "Introduced in Windows 8. A Windows Store app is running.")Upvotes: 22
Reputation: 99
Hooch's and ens' answers actually don't work on a multiple monitor system. That's because
The rectangle of the desktop window returned by GetWindowRect or GetClientRect is always equal to the rectangle of the primary monitor, for compatibility with existing applications.
See https://learn.microsoft.com/en-us/windows/desktop/gdi/multiple-monitor-system-metrics for reference.
The above means that if the window is fullscreen on a monitor that's not the primary monitor of the system, the coordinates (which are relative to the virtual screen) are completely different from the coordinates of the desktop window.
I fixed this with the following function:
bool isFullscreen(HWND windowHandle)
{
MONITORINFO monitorInfo = { 0 };
monitorInfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(MonitorFromWindow(windowHandle, MONITOR_DEFAULTTOPRIMARY), &monitorInfo);
RECT windowRect;
GetWindowRect(windowHandle, &windowRect);
return windowRect.left == monitorInfo.rcMonitor.left
&& windowRect.right == monitorInfo.rcMonitor.right
&& windowRect.top == monitorInfo.rcMonitor.top
&& windowRect.bottom == monitorInfo.rcMonitor.bottom;
}
Upvotes: 7
Reputation: 19185
Here's also a Java
JNA
implementation based on ens' answer:
public static boolean isFullScreen()
{
WinDef.HWND foregroundWindow = GetForegroundWindow();
WinDef.RECT foregroundRectangle = new WinDef.RECT();
WinDef.RECT desktopWindowRectangle = new WinDef.RECT();
User32.INSTANCE.GetWindowRect(foregroundWindow, foregroundRectangle);
WinDef.HWND desktopWindow = User32.INSTANCE.GetDesktopWindow();
User32.INSTANCE.GetWindowRect(desktopWindow, desktopWindowRectangle);
return foregroundRectangle.toString().equals(desktopWindowRectangle.toString());
}
Note that the toString()
comparison at the bottom is a small hack to avoid comparing 4 elements to each other.
Upvotes: 0
Reputation: 1098
A full implementation of Hooch's answer:
bool isFullscreen(HWND window)
{
RECT a, b;
GetWindowRect(window, &a);
GetWindowRect(GetDesktopWindow(), &b);
return (a.left == b.left &&
a.top == b.top &&
a.right == b.right &&
a.bottom == b.bottom);
}
Upvotes: 5
Reputation: 29673
hWnd = GetForegroundWindow();
RECT appBounds;
RECT rc;
GetWindowRect(GetDesktopWindow(), &rc);
Then check if that windows isn't desktop or shell. Simple if instruction.
if(hWnd =! GetDesktopWindow() && hWnd != GetShellWindow())
{
GetWindowRect(hWnd, &appBounds);
// Now you just have to compare rc to appBounds
}
This is written without testing.
Upvotes: 11