1.显示桌面可以调用X路径Application Data\Microsoft\Internet Explorer\Quick Launch\显示桌面.scf这个Windows Explorer Command文件,这个路径因人而异,需要获取。
2.也可以用Win键+M键激活
3.就是常用的枚举顶层(top-level)窗口EnumWindows获取所有最小化和可见状态的窗体句柄,然后用ShowWindow调用可用状态
具体VB实现如下
'窗体部分,四个按钮,名字看代码就知道。
Option Explicit
Private Sub cmdMin_Click()
Call EnumWindows(AddressOf EnumWindowsProc, 0&) '通过参数调用决定是最小化
cmdMin.Enabled = False
cmdMax.Enabled = True
End Sub
Private Sub cmdShowDeskTop_Click() '调用显示桌面.scf
Dim strBuffer As String
Dim strPath As String
strBuffer = String$(MAX_PATH, 0)
SHGetSpecialFolderPath Me.hwnd, strBuffer, CSIDL_APPDATA, 0
strPath = Left$(strBuffer, InStr(1, strBuffer, Chr$(0)) - 1)
strPath = strPath & "\Microsoft\Internet Explorer\Quick Launch\显示桌面.scf"
ShellExecute Me.hwnd, "Open", strPath, "", "", 1
cmdMin.Enabled = False
cmdMax.Enabled = True
End Sub
Private Sub cmdHot_Click()
DoAction (2) '组合热键显示桌面
cmdMin.Enabled = False
cmdMax.Enabled = True
End Sub
Private Sub cmdMax_Click()
Call EnumWindows(AddressOf EnumWindowsProc, 1&) '通过参数调用决定是最大化
cmdMin.Enabled = True
cmdMax.Enabled = False
End Sub
'模块部分
Option Explicit
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_VISIBLE = &H10000000
Private Const GWL_STYLE = (-16)
'EnumWindows是一个与EnumWindows或EnumDesktopWindows一起使用的应用程序定义的回调函数,它接收顶层窗口句柄。
'WNDENUMPROC定义一个指向这个回调函数的指针。EnumWindowsProc是应用程序定义函数名的位置标志符。
'参数:
'hwnd: 顶层窗口句柄。
'lParam: 指定在EnumWIndowsh或EnumDesktopWindows中的应用程序定义值。
'返回值:为继续列表,回调函数必须返回TRUE;若停止列表,它必须返回FALSE。
'备注:应用程序必须通过传递给EnumWindows或EnumDesktopWindows应用程序地址来注册这个回调函数。
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
'获得特定的路径名,调用显示桌面.scf
Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias _
"SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, _
ByVal lpszPath As String, ByVal nFolder As Long, ByVal fCreate As Long) As Long
Public Const CSIDL_APPDATA = &H1A
Public Const MAX_PATH = 260
' 查找与指定文件关联在一起的程序的文件名
'返回值Long,非零表示成功,零表示失败。会设置GetLastError
'参数 类型及说明
'hwnd Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框
'lpOperation String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它
'lpFile String,想用关联程序打印或打开一个程序名或文件名
'lpParameters String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数
'lpDirectory String,想使用的完整路径
'nShowCmd Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'keybd_event这个函数模拟了键盘行动
'参数 类型及说明
'bVk Byte,欲模拟的虚拟键码
'bScan Byte,键的OEM扫描码
'dwFlags Long,零;或设为下述两个标志之一
'KEYEVENTF_EXTENDEDKEY 指出是一个扩展键,而且在前面冠以0xE0代码
'KEYEVENTF_KEYUP 模拟松开一个键
'dwExtraInfo Long,通常不用的一个值。api函数GetMessageExtraInfo可取得这个值。允许使用的值取决于特定的驱动程序
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const VK_LWIN = &H5B
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_APPS = &H5D
Private Const VK_PLAY = &HFA
'该函数是EnumWindows的回调函数,EnumWindows函数将遍历的窗口句柄传递到hwnd参数中
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim lngStyle As Long
lngStyle = GetWindowLong(hwnd, GWL_STYLE)
Select Case lParam
Case 0
If ((lngStyle And WS_MINIMIZEBOX) = WS_MINIMIZEBOX) Then '如果窗口具有最小化按钮并且窗口是可见的就将该窗口最小化
If ((lngStyle And WS_VISIBLE)) Then
Call ShowWindow(hwnd, SW_MINIMIZE)
End If
End If
Case 1
If ((lngStyle And WS_MAXIMIZEBOX) = WS_MAXIMIZEBOX) Then '如果窗口具有最小化按钮并且窗口是可见的就将该窗口最小化
If ((lngStyle And WS_VISIBLE)) Then
Call ShowWindow(hwnd, SW_SHOWMAXIMIZED)
End If
End If
End Select
EnumWindowsProc = True
End Function
Public Sub DoAction(Index As Integer) '模拟按键
Dim VK_ACTION As Long
Select Case Index
Case 0
VK_ACTION = vbKeyE '打开资源管理器
Case 1
VK_ACTION = vbKeyF3 '查找文件
Case 2
VK_ACTION = vbKeyM '最小化所有窗口
Case 3
VK_ACTION = vbKeyR '运行程序
Case 4
VK_ACTION = VK_LWIN '弹出Win菜单
Case 5
VK_ACTION = &H5E '将计算机转入睡眠状态
Case 6
VK_ACTION = vbKeyF1 '执行Windows帮助
End Select
Call keybd_event(VK_LWIN, 0, 0, 0) '按下WIN键
Call keybd_event(VK_ACTION, 0, 0, 0) '用户按键
Call keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0) '模拟松开按键
End Sub
截图