函数参考


_WinAPI_SwitchDesktop

显示指定桌面并激活

#Include <WinAPIEx.au3>
_WinAPI_SwitchDesktop ( $hDesktop )

参数

$hDesktop 桌面句柄. 该桌面必须与当前窗口站进程相关.

返回值

成功: 返回 1
失败: 返回 0并设置@error非0

注意/说明

当桌面属于隐藏的窗口站时_WinAPI_SwitchDesktop()函数失败.
当由安全桌面, 如WinLogon和ScreenSaver桌面, 相关的进程调用时该函数也会失败.
与安全桌面相关的进程包括自定义的UserInit进程. 此类调用通常会以"拒绝访问"错误失败.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

Global $hDesktop, $hPrev, $pText, $tProcess, $tStartup

; Retrieve a handle to the current desktop and create a new desktop named "MyDesktop"
$hPrev = _WinAPI_GetThreadDesktop(_WinAPI_GetCurrentThreadID())
$hDesktop = _WinAPI_CreateDesktop('MyDesktop', BitOR($DESKTOP_CREATEWINDOW, $DESKTOP_SWITCHDESKTOP))
If Not $hDesktop Then
    MsgBox(16, 'Error', 'Unable to create desktop.')
    Exit
EndIf

; Switch to the newly created desktop
_WinAPI_SwitchDesktop($hDesktop)

; Run "calc.exe" on "MyDesktop" and wait until a process will not be closed by user
$pText = _WinAPI_CreateString('MyDesktop')
$tProcess = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup = DllStructCreate($tagSTARTUPINFO)
DllStructSetData($tStartup, 'Size', DllStructGetSize($tStartup))
DllStructSetData($tStartup, 'Desktop', $pText)
If _WinAPI_CreateProcess('', @SystemDir & '\calc.exe', 0, 0, 0, $CREATE_NEW_PROCESS_GROUP, 0, 0, DllStructGetPtr($tStartup), DllStructGetPtr($tProcess)) Then
    ProcessWaitClose(DllStructGetData($tProcess, 'ProcessID'))
EndIf

; Switch to previous desktop and close "MyDesktop"
_WinAPI_SwitchDesktop($hPrev)
_WinAPI_CloseDesktop($hDesktop)

; Free memory allocated for a string
_WinAPI_FreeMemory($pText)