Multiple desktop support in Windows


In my previous job I worked in the development team of Adobe FrameMaker. This product shipped on Sun Solaris, Windows and Mac. I worked parallely on a Windows box, a Sun Blade system and a PowerMac G5. So I got used to the best of all of them.


Solaris (and many other window managers) supports multiple desktops for each user and easy swtiching between them. I got used to have 4 desktops and spread my applications across them. One had all the coding stuff like the editors and debuggers and the other had email-client and browser and so on. I always felt unhappy that Windows didn't support this. It was much later when I came across the multi-desktop Win32 API's. After playing around a bit with them I was really surprised that Windows (XP/2000/2003) didn't expose this feature directly. Later I found Microsoft Power Toys for XP which supported multi-desktops. I assumed that Vista Window Manager will have this directly. But unfortunately it does not (see the feature list here). 


Using the Windows Desktop API's its extremely easy to create multiple desktops and switch between them. The following code creates a new desktop name MyDesk and launches Windows explorer on it and switches to the newly created desktop

IntPtr hDesk = Native.CreateDesktop("MyDesk", IntPtr.Zero,

IntPtr.Zero, 0, AccessRights, IntPtr.Zero);

STARTUPINFO si = new STARTUPINFO();
si.cb =
Marshal.SizeOf(si);
si.lpDesktop =
"MyDesk";
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
bool result = Native.CreateProcess(null, @"C:\WINDOWS\explorer.exe",
IntPtr.Zero, IntPtr.Zero,
true, NORMAL_PRIORITY_CLASS,
IntPtr.Zero, null, ref si, ref pi);

Native.SwitchDesktop(hDesk);


Check out http://www.pinvoke.net for the C# signatures for these Win32 api's. Similarly is easy to enumerate all the desktops

delegate bool EnumDesktopsDelegate(string desktop, IntPtr lParam);

private
bool DesktopEnum(string desktop, IntPtr lParam)
{
Console.WriteLine(desktop);
return true;
}
Native.EnumDesktops(windowStation,
new EnumDesktopsDelegate(DesktopEnum),
IntPtr.Zero);

Comments (15)

  1. leppie says:

    Thanks, will check it out!

    The $100 million question is: can you use that new desktop as a VNC source, so it can be emitted to another laptop/pc as a 3rd (or more) monitor?

  2. Gabe says:

    As I understand it, the type of virtual desktop support you see on X systems like the Sun is actually just using a single desktop with more pixels than the screen, and moving the origin to whichever "desktop" you want to see. So a 1024×768 monitor with 4 virtual desktops might be 2048×1536 with the origin usually at (0,0). If you want to switch to another desktop, the window manager just sets the origin to (1024,0) or (0,768) or (1024,768); however, you could switch to the middle by setting the origin to (512,384). To move a window to another desktop, just drag it to where you want it.

    This is the way virtual desktops work on X, and the way they’ve worked on Windows. In fact, I had a virtual window manager that worked this way back in 1992.

    However, this is not how multiple desktops work. With multiple desktops, each window is confined to the desktop where it was created, and cannot communicate (send messages) with windows on other desktops. For example, the windows login dialog is on one desktop while the rest of your programs on on another desktop. This means that there’s no way for a program you’re running to capture keystrokes from the login dialog. This is probably not how you want your desktops to work, though.

    I’m not sure why Windows doesn’t ship with virtual desktop support, but I guess it would confuse most people ("where did my desktop go?"), and isn’t really necessary. While I’ve used virtual window managers frequently on Unix, I haven’t really found myself wanting them on Windows. I tried one many years ago, and never found it useful.

  3. Adam says:

    I too am used to having multiple desktops on UNIX, and tried moving to them on Windows. There were just way too many bugs though.

    Visual Studio just doesn’t like it. For some reason it seems to put two items in the alt-tab window cycler when I had multiple desktops turned on. Also, if I was debugging a program and was stepping through code, then attempting to switch desktops (say, to look up something on google related to the code I was stepping through) would pretty much hang everything. ctrl+alt+del could bring up the task manager, but trying to kill a task was awkward and didn’t always work.

    Word seemed to decide to change which desktop it was on every now and then, which was a pain.

    The taskbar desktop switcher just had to have the desktop thumbnails in a single row, insead of being able to put them on two rows if you have a double-height taskbar, taking up too much horiz space.

    Umm, there were other things as well, but I’m back to a single desktop on Windows. As much as I hate and feel constrained by it, I prefer it to the alternative.

  4. daniel says:

    Anybody else have trouble with firefox using this method?  I can’t use firefox on more than one desktop because firefox can’t start another instance if it sees that it’s currently running, and it can’t interact with the previous instance if it’s on a different desktop.

  5. iqbal says:

    hi im looking for a solution whereby i can use 1 computer and split it to be used by children in a primary school in uganda. i want to have say 10 usrs all sharing the same desktop but using different keyboards and mouse. do you know of a free solution to this. or one which is resonably priced

  6. Coding4Fun says:

    Having multiple monitors makes life easier in my opinion but even with my current work setup, I need

  7. joe says:

    i have looked here and many other places. i have implemented the use of 4 desktops in my project and i can switch between them very easily, but i want to have a single app to switch them rather than having 4 instances of the same app. also i would like to have a screen shot of each desktop on the app… my major question is, would a windows service be able to communicate between these desktops?

  8. veerkumar patil(India) says:

    Hello Sir,I am last year Computer Engg Student

    I am working on the project of Remote Computer administration . I am trying  to create Desktop

    in C# but i found that there is no any Desktop Supporting class I ve read ur article on multiple desktop creation

    Can u plz give me the idea regarding Creating Desktop

  9. Veerkumar says:

    I forgot to send my E-mail ID

    veer1109@rediffmail.com

  10. Yossi says:

    I am looking for Vista API for enabling multi monitor.

    I have laptop which is connected to external graphic device, I want an API to extend/set primary/de-extend my desktop to this graphic device monitor

    DO you know which API control weather specific monitor is extended or weather specific monitor is primary ?

  11. Leeoz says:

    Hi,

    I’m trying to create an APP which creates a new desktop.

    Where can I find more info on how to control the new Desktop including: Running apps, Creating Icons, Closing the Desktop?

    Tnx,

    Leeoz

  12. amin says:

    Hi,

    I see your site and use of your information, Thanks for this,

    I am programmer, and I am developing a program that should show some forms in different LCD, in the same time, I use NVidia to have multiple desktop and after show the form, I send it to another desktop then, set the window state and windowborderstyle to sake the form full screen from Main(first) form, but the form in the second desktop show in background of the first desktop, and it doesn’t hide with refresh,

    just should change the desktop then comeback to first desktop.

    and I don’t know what should I do?

    I will be glad if you help me.

    amin shahab

  13. Stoyan Sabev says:

    Mark Roussinovich's desktops app gives you four desktops and easy way to switch between them:

    technet.microsoft.com/…/cc817881.aspx

Skip to main content