简体   繁体   中英

Persistent window across multiple windows 10 virtual desktops?

I have C# WPF program with multiple windows. I've added support for windows 10 desktops, but the users would like some windows to stay on the screen when moving between desktops.

For example if window A is opened on the first desktop and they flip to the second desktop they want window A to stay in the same location on the new desktop.

The only functions I'm aware of are from the VirtualDesktopManager:

GetWindowsDesktopId()
IsWindowOnCurrentVirtualDesktop()
MoveWindowToDesktop()

Is there a way to do this?

Also is there a way to detect when a desktop flip has been initiated? Because if so I could always call IsWindowOnCurrentVirtualDesktop() and if the answer is no I could call MoveWindowToDesktop() to place it there. Seems like a bit of a hack, but would get the job done if i had a way to detect the desktop change.

You can detect the virtual desktop change, found a nice GitHub project with the necessary code as well as more functions that deal with Virtual Desktops in Windows 10.

Virtual Desktop GitHub

To get the event and simulate the window staying on every desktop you can do the following.

VirtualDesktop.CurrentChanged += (o, e) =>
{
    this.Dispatcher.Invoke(() =>
    {
        var h = new WindowInteropHelper(this).Handle;

        if (!VirtualDesktopHelper.IsCurrentVirtualDesktop(h))
        {
            this.MoveToDesktop(VirtualDesktop.Current);
        }
    });
};

The Dispatcher.Invoke is necessary because the event is on a different thread then the UI one, so the call must be marshaled to the UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM