以编程方式显示桌面
Programmatically show the desktop
我正在编写 Windows 表单应用程序,我需要以编程方式返回桌面。
我试过这段代码,但它不起作用:
using System;
using System.Windows.Forms;
private void ToggleDesktop() {
SendKeys.Send("^({ESC}D)"); //<-- Semantic error, Should simulate: WIN+D
}
有可能吗?
您可以使用 Shell32.dll
windows 程序集来做到这一点。
只需添加对 C:\Windows\System32\Shell32.dll
的引用,然后转到引用属性并在 Embed Interop Types
附近放置一个 False
(因为 class 您将使用是 ShellClass
,即互操作。
现在,就这么简单
Shell32.ShellClass objShel = new Shell32.ShellClass();
objShel.ToggleDesktop();
也可以(在Windows 8.1下测试):
将 COM 引用添加到 "Microsoft Shell Controls and Automation"
(c:\windows\system32\shell32.dll)
然后:
using Shell32;
Shell shellObject = new Shell();
shellObject.ToggleDesktop(); // WinXp: ((Shell32.IShellDispatch4)shellObject).ToggleDesktop();
我正在编写 Windows 表单应用程序,我需要以编程方式返回桌面。
我试过这段代码,但它不起作用:
using System;
using System.Windows.Forms;
private void ToggleDesktop() {
SendKeys.Send("^({ESC}D)"); //<-- Semantic error, Should simulate: WIN+D
}
有可能吗?
您可以使用 Shell32.dll
windows 程序集来做到这一点。
只需添加对 C:\Windows\System32\Shell32.dll
的引用,然后转到引用属性并在 Embed Interop Types
附近放置一个 False
(因为 class 您将使用是 ShellClass
,即互操作。
现在,就这么简单
Shell32.ShellClass objShel = new Shell32.ShellClass();
objShel.ToggleDesktop();
也可以(在Windows 8.1下测试):
将 COM 引用添加到 "Microsoft Shell Controls and Automation"
(c:\windows\system32\shell32.dll)
然后:
using Shell32;
Shell shellObject = new Shell();
shellObject.ToggleDesktop(); // WinXp: ((Shell32.IShellDispatch4)shellObject).ToggleDesktop();