我需要检测某个应用程序是否正在全屏模式下运行。如果是,则我必须停止我的应用程序。那么,如何检测呢? p.s. Win32 C++
所有其他答案都相当繁琐。
Windows Vista、Windows 7及更高版本支持SHQueryUserNotificationState()
:
QUERY_USER_NOTIFICATION_STATE pquns;
SHQueryUserNotificationState(&pquns);
从这个“通知状态”,可以推断出全屏状态。基本上,当有一个应用在全屏模式下运行时,Windows会报告一个“繁忙”的通知状态。
QUNS_NOT_PRESENT
- 不是全屏(机器锁定/屏幕保护程序/用户切换等情况)QUNS_BUSY
– 全屏(F11全屏,以及我尝试过的所有视频游戏都使用此选项)QUNS_RUNNING_D3D_FULL_SCREEN
– 全屏(Direct3D应用程序正在以独占模式运行,即全屏)QUNS_PRESENTATION_MODE
– 全屏(用于显示演示文稿的特殊模式,全屏显示)QUNS_ACCEPTS_NOTIFICATIONS
– 不是全屏QUNS_QUIET_TIME
– 不是全屏QUNS_APP
– 可能是全屏(不确定:“在Windows 8中引入。正在运行Windows商店应用程序。”)hWnd = GetForegroundWindow();
RECT appBounds;
RECT rc;
GetWindowRect(GetDesktopWindow(), &rc);
然后检查那个窗口不是桌面或者 shell。 简单的 if 语句。
if(hWnd =! GetDesktopWindow() && hWnd != GetShellWindow())
{
GetWindowRect(hWnd, &appBounds);
// Now you just have to compare rc to appBounds
}
这是未经测试的编写。
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;
}
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);
}
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());
}
toString()
比较是一个小技巧,可以避免相互比较4个元素。