5

Concerning Windows 10 and its new virtual desktop feature, is there a way to determine which virtual desktop a particular window belongs to? And, which virtual desktop is active?

The problem can be seen using the Snipping Tool. Open the tool and select a New / Window Snip. As you move the mouse around, the snipping tool highlights areas where there is no window, but there is a window at that location on another virtual desktop.

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

Snipping Tool doesn't know which virtual desktop a particular window is on.

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

Sorry, my status isn't high enough to insert images or include more links.

CaptureWiz
  • 1,685
  • 1
  • 15
  • 15

1 Answers1

4

The Windows SDK Support Team Blog posted a C# demo to switch Desktops via IVirtualDesktopManager:

    [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);
            }
        }
    }

Call GetWindowDesktopId to get the Desktop GUID.

SAMPro
  • 1,068
  • 1
  • 15
  • 39
magicandre1981
  • 27,895
  • 5
  • 86
  • 127
  • 1
    Googling for `IVirtualDesktopManager` gives the MSDN page: https://msdn.microsoft.com/en-us/library/windows/desktop/Mt186440(v=VS.85).aspx – Mark Ransom Sep 11 '15 at 16:16
  • Thanks, magicandre, for explaining how and where you got that answer, it was hard work, not magic :) – CaptureWiz Sep 12 '15 at 20:20
  • I saw it in the RSS feed of the blog. Also subscribe to it to get notified about some nice samples, Hotfixes and other tricks. – magicandre1981 Sep 13 '15 at 05:48