最近项目中需要判断窗口是否在当前虚拟桌面的需求(Windows平台)。在网上找到了下面的方法,亲测可以实现,分享给大家。如果你有更好的方法,请在留言区分享吧。
IVirtualDesktopManager
实例的IsWindowOnCurrentVirtualDesktop
提供窗口是否在当前活动的虚拟桌面上。
IServiceProvider* pServiceProvider = NULL;
HRESULT hr = ::CoCreateInstance(
CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER,
__uuidof(IServiceProvider), (PVOID*)&pServiceProvider);
if (SUCCEEDED(hr))
{
IVirtualDesktopManager *pDesktopManager = NULL;
hr = pServiceProvider->QueryService(__uuidof(IVirtualDesktopManager), &pDesktopManager);
if (SUCCEEDED(hr))
{
BOOL bIsOnCurrentDesktop = FALSE;
hr = pDesktopManager->IsWindowOnCurrentVirtualDesktop(hWnd, &bIsOnCurrentDesktop);
if (SUCCEEDED(hr))
{
// use bIsOnCurrentDesktop as needed...
}
pDesktopManager->Release();
}
pServiceProvider->Release();
}
- 代码出处链接: https://stackoverflow.com/questions/40196874/how-to-use-the-ivirtualdesktopmanager-interface-in-c-win32
IVirtualDesktopManager
官方文档: https://docs.microsoft.com/zh-cn/windows/win32/api/shobjidl_core/nn-shobjidl_core-ivirtualdesktopmanager?redirectedfrom=MSDN