我正在尝试在单独的桌面上打开WPF窗口。在Windows 10中,下面的代码按预期工作并显示此窗口:
但是,在Windows 7中,文本未显示,Windows壁纸渗透:
我认为代码很简单:
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Runtime.InteropServices;
class Program
{
const uint GENERIC_ALL = 0x10000000;
[DllImport("kernel32.dll")] static extern uint GetCurrentThreadId();
[DllImport("user32.dll")] static extern IntPtr GetThreadDesktop(uint dwThreadId);
[DllImport("user32.dll")] static extern IntPtr CreateDesktop(string desktopName, IntPtr device, IntPtr deviceMode, uint flags, uint accessMask, IntPtr attributes);
[DllImport("user32.dll")] static extern bool SetThreadDesktop(IntPtr hDesktop);
[DllImport("user32.dll")] static extern bool CloseDesktop(IntPtr hDesktop);
[DllImport("user32.dll")] static extern bool SwitchDesktop(IntPtr hDesktop);
[STAThread]
public static void Main()
{
var btn = new Button() { Content = "Open secure desktop" };
btn.Click += (s, e) => SecureDialog();
var win = new Window() { Content = btn };
new Application().Run(win);
}
static void SecureDialog()
{
var hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
var hNewDesktop = CreateDesktop("My desktop", IntPtr.Zero, IntPtr.Zero, 0, GENERIC_ALL, IntPtr.Zero);
SwitchDesktop(hNewDesktop);
var thread = new Thread( () => SecureDesktopThread(hNewDesktop) );
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
SwitchDesktop(hOldDesktop);
CloseDesktop(hNewDesktop);
}
static void SecureDesktopThread(IntPtr hNewDesktop)
{
SetThreadDesktop(hNewDesktop);
var win = new Window() { Content = "Hello World!", FontSize=30 };
win.ShowDialog();
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown();
}
}
那么,为什么Win7上的行为不同?我在这里错过了什么吗?