跨多个Windows 10虚拟桌面的持久窗口? [英] Persistent window across multiple windows 10 virtual desktops?

查看:41
本文介绍了跨多个Windows 10虚拟桌面的持久窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有多个窗口的C#WPF程序.我已经添加了对Windows 10桌面的支持,但是用户希望在桌面之间移动时某些窗口保留在屏幕上.

例如,如果在第一个桌面上打开了窗口A,而他们又翻转到第二个桌面,则希望窗口A停留在新桌面上的相同位置.

我唯一知道的功能来自VirtualDesktopManager:

  GetWindowsDesktopId()IsWindowOnCurrentVirtualDesktop()MoveWindowToDesktop() 

有没有办法做到这一点?

还有什么方法可以检测何时启动桌面翻转?因为如果是这样,我总是可以调用IsWindowOnCurrentVirtualDesktop(),如果答案是否定的,我可以调用MoveWindowToDesktop()来放置它.似乎有点骇人听闻,但如果我有办法检测桌面更改,就可以完成工作.

解决方案

您可以检测到虚拟桌面的变化,找到一个不错的GitHub项目,其中包含必要的代码以及更多处理Windows 10中虚拟桌面的功能.

虚拟桌面GitHub

要获取事件并模拟驻留在每个桌面上的窗口,您可以执行以下操作.

  VirtualDesktop.CurrentChanged + =(o,e)=>{this.Dispatcher.Invoke(()=>{var h = new WindowInteropHelper(this).Handle;如果(!VirtualDesktopHelper.IsCurrentVirtualDesktop(h)){this.MoveToDesktop(VirtualDesktop.Current);}});}; 

Dispatcher.Invoke是必需的,因为该事件位于与UI线程不同的线程上,因此必须将调用编组到UI线程.

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.

这篇关于跨多个Windows 10虚拟桌面的持久窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆