The CreateDesktop function creates a new desktop on the window station associated with the calling process.
Before calling CreateDesktop_() you need to associate a window station with your process. This is very easy if you've got the handle to one, just do this:
But first you need the handle to a window station. This is almost tricky, but not really, since I tell you that the name of the standard window station is WinSta0.
Code: Select all
hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ALL)
(
Here's how to get all window stations in the system:Code: Select all
Procedure EnumWindowStationProc(Name.s, lParam.l)
Debug Name
ProcedureReturn 1
EndProcedure
EnumWindowStations_(@EnumWindowStationProc(), 0)
)[/color]
It is now pretty straightforward to create a new desktop and switch to it:
Code: Select all
#DESKTOP_ALL = #DESKTOP_WRITEOBJECTS | #DESKTOP_SWITCHDESKTOP | #DESKTOP_READOBJECTS | #DESKTOP_JOURNALRECORD | #DESKTOP_JOURNALPLAYBACK | #DESKTOP_HOOKCONTROL | #DESKTOP_ENUMERATE | #DESKTOP_CREATEWINDOW | #DESKTOP_CREATEMENU
hDesk = CreateDesktop_("Kool Desktop 2", 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #DESKTOP_ALL, 0)
SwitchDesktop_(hDesk)
Then a little delay:
Just one "little" problem: How do we get back?? First we need the handle to the standard desktop. This one's named "Default".
Code: Select all
hDefaultDesk = OpenDesktop_("Default", #DF_ALLOWOTHERACCOUNTHOOK, 0, #DESKTOP_SWITCHDESKTOP)
(
See all desktops in a window station:Code: Select all
Procedure EnumDesktopProc(Name.s, lParam.l)
Debug Name
ProcedureReturn 1
EndProcedure
hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ENUMDESKTOPS)
EnumDesktops_(hWinSta, @EnumDesktopProc(), 0)
CloseWindowStation_(hWinSta)
)[/color]
Then we switch back:
And clean up after us (important):
Code: Select all
CloseDesktop_(hDesk) ; This DELETES the desktop we created
CloseDesktop_(hDefaultDesk) ; This closes the handle to the default desktop
CloseWindowStation_(hWinSta) ; This closes the handle to the window station.
Full code:
Code: Select all
#DESKTOP_ALL = #DESKTOP_WRITEOBJECTS | #DESKTOP_SWITCHDESKTOP | #DESKTOP_READOBJECTS | #DESKTOP_JOURNALRECORD | #DESKTOP_JOURNALPLAYBACK | #DESKTOP_HOOKCONTROL | #DESKTOP_ENUMERATE | #DESKTOP_CREATEWINDOW | #DESKTOP_CREATEMENU
;Show an error
Procedure Abort(s.s)
MessageRequester("", "Error: "+s.s)
End
EndProcedure
;Check a result and ev. abort
Procedure Chk(a.l, s.s)
;If a is false, abort.
;If a is false and s contains an error message, show it before abort.
If Not(a)
If s.s
Abort(s.s)
Else
End
EndIf
EndIf
EndProcedure
hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ALL)
Chk( SetProcessWindowStation_(hWinSta) , "Failed to set window station")
hDefaultDesk = OpenDesktop_("Default", #DF_ALLOWOTHERACCOUNTHOOK, 0, #DESKTOP_SWITCHDESKTOP)
Chk(hDefaultDesk, "Failed to open default desktop")
hDesk = CreateDesktop_("My Desktop 2", 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #DESKTOP_ALL, 0)
Chk( hDesk, "Failed to create desktop")
Chk( SwitchDesktop_(hDesk), "Failed to switch desktop")
Delay(2000)
Chk( SwitchDesktop_(hDefaultDesk), "Failed to switch desktop")
CloseDesktop_(hDesk)
CloseDesktop_(hDefaultDesk)
CloseWindowStation_(hWinSta)