【问题标题】:In Windows 10, how can we determine which virtual desktop a window belongs to?在 Windows 10 中,我们如何确定一个窗口属于哪个虚拟桌面?
【发布时间】:2015-03-14 16:50:20
【问题描述】:

关于 Windows 10 及其新的虚拟桌面功能,有没有办法确定特定窗口属于哪个虚拟桌面?以及,哪个虚拟桌面处于活动状态?

可以使用截图工具查看问题。打开该工具并选择一个新建/窗口截图。当您四处移动鼠标时,截图工具会突出显示没有窗口的区域,但在另一个虚拟桌面上的该位置有一个窗口。

In this picture, the Snipping Tool is highlighting an empty spot.

截图工具不知道特定窗口在哪个虚拟桌面上。

Here's the same question on MSDN Forums, unanswered, but with lots of additional detail.

抱歉,我的状态不够高,无法插入图片或包含更多链接。

【问题讨论】:

  • 尚未发布适用于 Windows 10 的 SDK。
  • 好吧,如果 Windows 10 将于今年夏天发布,这并没有给我们太多时间来解决问题:-(

标签: windows screen-capture windows-10 virtual-desktop


【解决方案1】:

Windows SDK Support Team Blog 通过IVirtualDesktopManager 发布了C# demo to switch Desktops

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("a5cd92ff-29be-454c-8d04-d82879fb3f1b")]
    [System.Security.SuppressUnmanagedCodeSecurity]
    public interface IVirtualDesktopManager
    {
    [PreserveSig]
    int IsWindowOnCurrentVirtualDesktop(
        [In] IntPtr TopLevelWindow,
        [Out] out int OnCurrentDesktop
        );
    [PreserveSig]
    int GetWindowDesktopId(
        [In] IntPtr TopLevelWindow,
        [Out] out Guid CurrentDesktop
        );

    [PreserveSig]
    int MoveWindowToDesktop(
        [In] IntPtr TopLevelWindow,
        [MarshalAs(UnmanagedType.LPStruct)]
        [In]Guid CurrentDesktop
        );
    }
    
    [ComImport, Guid("aa509086-5ca9-4c25-8f95-589d3c07b48a")]
    public class CVirtualDesktopManager
    {
    
    }
    public class VirtualDesktopManager
    {
        public VirtualDesktopManager()
        {
            cmanager = new CVirtualDesktopManager();
            manager = (IVirtualDesktopManager)cmanager;
        }
        ~VirtualDesktopManager()
        {
            manager = null;
            cmanager = null;
        }
        private CVirtualDesktopManager cmanager = null;
        private IVirtualDesktopManager manager;
    
        public bool IsWindowOnCurrentVirtualDesktop(IntPtr TopLevelWindow)
        {
            int result;
            int hr;
            if ((hr = manager.IsWindowOnCurrentVirtualDesktop(TopLevelWindow, out result)) != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }
            return result != 0;
        }
    
        public Guid GetWindowDesktopId(IntPtr TopLevelWindow)
        {
            Guid result;
            int hr;
            if ((hr = manager.GetWindowDesktopId(TopLevelWindow, out result)) != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }
            return result;
        }
    
        public void MoveWindowToDesktop(IntPtr TopLevelWindow, Guid CurrentDesktop)
        {
            int hr;
            if ((hr = manager.MoveWindowToDesktop(TopLevelWindow, CurrentDesktop)) != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
    }

调用 GetWindowDesktopId 获取桌面 GUID。

【讨论】:

  • 谷歌搜索 IVirtualDesktopManager 得到 MSDN 页面:msdn.microsoft.com/en-us/library/windows/desktop/…
  • 谢谢,magicandre,你解释了你是如何以及在哪里得到这个答案的,这是一项艰苦的工作,而不是魔法:)
  • 我在博客的 RSS 提要中看到了它。还可以订阅它以获得有关一些不错的示例、修补程序和其他技巧的通知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2015-11-09
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多