Virtual Desktops

Just starting out? Need help? Post your questions and find answers here.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Virtual Desktops

Post by Num3 »

I've stripped this from a Delphi complex source code.

It works in Delphi, but not in PB, can anyone help !?

Code: Select all

DesktopName.s="test 1"

Desk=CreateDesktop_(DesktopName, 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #MAXIMUM_ALLOWED, 0)
Debug Desk
Debug SwitchDesktop_(Desk)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

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:

Code: Select all

SetProcessWindowStation(hWinSta)
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:

Code: Select all

Delay(2000)
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:

Code: Select all

SwitchDesktop_(hDefaultDesk)
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)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Duh!

Exactly! I was missing the window station .

Thanks!

---EDIT---

Too weird!

Your code works great, but no new desktop appear, only windows background and mouse in wait...

After the 2 secs, back to normal :lol:
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Nice avatar :lol:
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Num3 wrote:... only windows background and mouse in wait...
May I introduce: This it your new desktop :shock:

And yes, it's totally empty :D
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

fsw wrote:
Num3 wrote:... only windows background and mouse in wait...
May I introduce: This it your new desktop :shock:

And yes, it's totally empty :D
+1
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Too weird, if i run the Vdesktop program and after that the PB code it works :shock:

Num3 Slaps MS and drack :P
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

you missed to define the constant, so here we go... :wink:

Code: Select all

#WINSTA_ALL = #WINSTA_ACCESSCLIPBOARD | #WINSTA_ACCESSGLOBALATOMS | #WINSTA_CREATEDESKTOP | #WINSTA_ENUMDESKTOPS | #WINSTA_ENUMERATE | #WINSTA_EXITWINDOWS | #WINSTA_READATTRIBUTES | #WINSTA_READSCREEN | #WINSTA_WRITEATTRIBUTES | #DELETE | #READ_CONTROL | #WRITE_DAC | #WRITE_OWNER
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

howto create a window on the empty desktop?
Post Reply