Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Windows 10, how can we determine which virtual desktop a window belongs to?

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.

like image 878
CaptureWiz Avatar asked Sep 28 '22 23:09

CaptureWiz


1 Answers

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.

like image 58
magicandre1981 Avatar answered Oct 27 '22 11:10

magicandre1981