Persistent window across multiple windows 10 virtual desktops?

ぃ、小莉子 提交于 2019-12-01 09:51:55

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33548454/persistent-window-across-multiple-windows-10-virtual-desktops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!