c# - 在虚拟桌面之间移动正在运行的应用程序

标签 c#

我已经阅读了与虚拟桌面相关的堆栈溢出文章,下面是与虚拟桌面相关的链接,但它没有解决我的问题。

“如何在默认桌面和 Winlogon 桌面之间切换进程?” How to switch a process between default desktop and Winlogon desktop?

“在 Windows 中的桌面之间移动应用程序” Moving applications between desktops in Windows

我在一个桌面上运行了 WPF 应用程序,当我切换到另一个桌面时,我想将该应用程序移动到另一个桌面。我已经应用了下面提到的文章中提到的代码。

{代码}

Debug.Write("MoveTONewDesktop ........");
IntPtr hWinSta0 = OpenWindowStation("WinSta0", false, ACCESS_MASK.MAXIMUM_ALLOWED);
Debug.Write("Windows Station Pointer "+ hWinSta0.ToInt32());
if (null == hWinSta0) { }

hWinSta0 = SetProcessWindowStation(hWinSta0);
Debug.Write("SetProcessWindowStation " + hWinSta0.ToInt32());

IntPtr hDesk = OpenDesktop("ABCD", 0, false, ACCESS_MASK.MAXIMUM_ALLOWED);
Debug.Write("OpenDesktop " + hDesk.ToInt32());
if (null == hDesk) { }

bool result = SwitchDesktop(hDesk);
Debug.Write("SwitchDesktop " + result);

bool bSuccess = SetThreadDesktop(hDesk);
Debug.Write("SetThreadDesktop " + bSuccess);
if (!bSuccess)
{
    Debug.Write("Get Last WIn32 Error " + Marshal.GetLastWin32Error());
    System.Console.WriteLine(Marshal.GetLastWin32Error());
}

if (hDesk != null) { CloseDesktop(hDesk); }
if (hWinSta0 != null) { CloseWindowStation(hWinSta0); }

在调试时,我看到打印了正确的句柄并且我的桌面被切换了。 setThreadDesktop 也显示了真正的值(value),但我的应用程序保留在旧桌面上,不会移动到新桌面。我的应用程序没有从一个桌面移动到另一个桌面的原因可能是什么。我错过了什么吗?请帮助...

感谢和问候, 阿舒

最佳答案

根据您的描述,我假设您的应用程序在创建它的“原始”桌面上已经至少有一个窗口。 MSDN 告诉我们:

The SetThreadDesktop function will fail if the calling thread has any windows or hooks on its current desktop (unless the hDesktop parameter is a handle to the current desktop).

但是你写到SetThreadDesktop成功了。对此的唯一解释是您应用 SetThreadDesktop 的线程没有任何 Hook 或窗口,因此它不是具有您已经显示的 UI 的线程。

不幸的是,在 Windows 上无法将窗口从一个桌面移动到另一个桌面。您最多只能在特定桌面上创建窗口。

您可能认为虚拟桌面管理器应用程序有解决方法。事实上,大多数 Windows 上的这些应用程序只是模仿多桌面行为,但它们只是在 Winsta0\default 桌面上显示/隐藏相应的窗口、任务栏、背景等。 其中一个异常(exception)是 SysInternals 的 Desktops 应用程序。但是你猜怎么着,他们遇到了同样的问题:

Desktops reliance on Windows desktop objects means that it cannot provide some of the functionality of other virtual desktop utilities, however. For example, Windows doesn't provide a way to move a window from one desktop object to another

关于c# - 在虚拟桌面之间移动正在运行的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20355104/

相关文章:

c# Excel 数据透视表中的折叠字段

c# - 无法创建类的实例

c# - GridViewColumn 内容和 VerticalAlignment

c# - 如何使 .net HttpClient 使用 http 2.0?

c# - 没有文件扩展名的C#搜索结果

c# - 最佳实践 LongRunning 任务创建

C# 泛型方法的奇怪内联行为 - 可能的错误

c# - SQL 中的字符串函数而不是 C#

C# - 遍历对和匹配

c# - MonoTouch 对话框的 UITableView。如何处理水龙头?