CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    14

    Exclamation Virtual Desktops

    Hello guys, my first post here.

    Recently, I've discovered programs that are capabale of displaying multiple desktops. Before I actually saw the program in action (Cube DeskTop) I thought this was completely impossible to do on Windows... infact I thought performing any sort of drawing (GDI and even Direct3D) on the desktop was impossible. So my question is, what functions are used to achieve these "effects"?

    Thanks guys.

  2. #2
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Virtual Desktops

    The multiple desktop capability of windows has been there since windows NT, MS just did not make any tool to utilize this capability. But they use it in logon screens, hidden desktops for services, etc.

    Here's a tool I made several years ago that creates and switches desktop. If the desktop is created for the first time, it will launch explorer.exe so you can have something to work with. You can run it like
    Code:
    SETDSKTP.EXE desktop1     ; creates and switch to a new desktop named "desktop1"
    SETDSKTP.EXE Default      ; switch to the original desktop created when you logon
    Make sure you save all your work first before trying the program in case something went wrong

    Code:
    #include <windows.h>
    #include <tchar.h>
    
    BOOL CALLBACK EnumProcFindTray(HWND hWnd, LPARAM lParam)
    {
       BOOL bRetval = TRUE;
       TCHAR szWndClass[128];
       if (GetClassName(hWnd, szWndClass, sizeof(szWndClass) / sizeof(TCHAR))) {
          if (0 == lstrcmpi(_T("Shell_TrayWnd"), szWndClass)) {
             *reinterpret_cast<BOOL*>(lParam) = TRUE;
             bRetval = FALSE;
          }
       }
       return bRetval;
    }
    
    int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
    {
       if (__argc > 1) {
          HDESK hDesk = OpenDesktop(__argv[1], 0, FALSE, DESKTOP_SWITCHDESKTOP);
          if (!hDesk) hDesk = CreateDesktop(__argv[1], NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, 0x1ff, NULL);
          if (hDesk) {
             BOOL hHasTrayWnd = FALSE;
             EnumDesktopWindows(hDesk, EnumProcFindTray, reinterpret_cast<LPARAM>(&hHasTrayWnd));
             if (!hHasTrayWnd) {
                PROCESS_INFORMATION pi;
                STARTUPINFO si = { sizeof(si) };
                GetStartupInfo(&si);
                si.lpDesktop = __argv[1];
                if (CreateProcess(NULL, _T("explorer.exe"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
                   CloseHandle(pi.hThread);
                   CloseHandle(pi.hProcess);
                   hHasTrayWnd = TRUE;
                }
             }
             if (hHasTrayWnd) SwitchDesktop(hDesk);
             CloseDesktop(hDesk);
          }
       }
       return 0;
    }
    Hope it will help you

  3. #3
    Join Date
    Apr 2009
    Posts
    14

    Re: Virtual Desktops

    Ah yes! Thanks man! It does help . I already had found some information on this topic on another site, but it didn't make much sense till I read some of your code and what you said.

    Just for people who would also like more information: The desktop you see when you start your computer isn't the only desktop on the computer, apart from that desktop there are to others. The one that you do see is called the "Default" desktop, when you press CTRL, ALT and DELETE in conjuction you get the "WinLogon" desktop and finally there is the screen saver desktop... if you want me to explain what a screen saver is please stop learning C++ and get a basic computer book .

    WARNING: Do not download his code, it wipes your entire hard-drive and your memory, from your brain not RAM. Just kidding, haha!

    Now seriously back to topic: Hmm, thanks for the easy to understand code! . My last question for this thread is... how do programs like Cube DeskTop manage to create the cube?... well actually that can be easily done using DirectX (more specifically Direct3D)... I meant how do they put every desktop's image in a cube format? or any format for that matter...

  4. #4
    Join Date
    Apr 2009
    Posts
    14

    Re: Virtual Desktops

    Well, I decided to start with something simple but the test hasn't gone and done what I expected it to.

    Code:
    #include <windows.h>
    
    #define SysBorderLR GetSystemMetrics(SM_CXBORDER)
    #define SysBorderTB GetSystemMetrics(SM_CYBORDER)
    #define SearchDesktopButton 101
    
    HINSTANCE hInstance = NULL;
    
    HWND MainWindow = NULL;
    HWND DesktopLB = NULL;
    HWND SearchDesktopsB = NULL;
    
    BOOL CALLBACK DesktopEnum(LPTSTR DesktopName, LPARAM lParam);
    LRESULT CALLBACK MainWNDProc(HWND MainWindow, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
    {
    
     WNDCLASSEX WndClass;
     MSG Msg;
    
     WndClass.cbSize = sizeof(WNDCLASSEX);
     WndClass.cbClsExtra = 0;
     WndClass.cbWndExtra = 0;
     WndClass.style = CS_HREDRAW | CS_VREDRAW;
     WndClass.hInstance = hInstance;
     WndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     WndClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
     WndClass.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
     WndClass.lpfnWndProc = MainWNDProc;
     WndClass.lpszMenuName = NULL;
     WndClass.lpszClassName = "MainWNDClass";
    
     RegisterClassEx(&WndClass);
    
     MainWindow = CreateWindowEx(NULL, WndClass.lpszClassName, "Welcome", WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |  WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 235, 480, NULL, NULL, hInstance, NULL);
    
     ShowWindow(MainWindow, nCmdShow);
     UpdateWindow(MainWindow);
    
     while(GetMessage(&Msg, NULL, NULL, NULL) > 0)
     {
    
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    
     }
    
     return Msg.wParam;
    
    }
    
    BOOL CALLBACK DesktopEnum(LPTSTR DesktopName, LPARAM lParam)
    {
    
     SendMessage(DesktopLB, LB_ADDSTRING, NULL, (LPARAM)DesktopName);
    
     return TRUE;
    
    }
    
    LRESULT CALLBACK MainWNDProc(HWND MainWindow, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    
     switch(Msg)
     {
    
      case WM_CREATE:
    
       DesktopLB = CreateWindowEx(NULL, "LISTBOX", "DesktopLB", WS_CHILD | WS_VISIBLE | LBS_SORT, 15, 15, 200, 100, MainWindow, NULL, hInstance, NULL);
       SearchDesktopsB = CreateWindowEx(NULL, "BUTTON", "Search for Desktops", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 15, 120, 200, 20, MainWindow, (HMENU)SearchDesktopButton, hInstance, NULL);
    
      break;
    
      case WM_COMMAND:
    
       switch(LOWORD(wParam))
       {
     
        case SearchDesktopButton:
    
    	 while(SendMessage(DesktopLB, LB_GETCOUNT, NULL, NULL) != 0) SendMessage(DesktopLB, LB_DELETESTRING, 0, NULL);
         
    	 EnumDesktops(GetProcessWindowStation(), DesktopEnum, NULL);
    
    	break;
    
       }
    
      break;
    
      case WM_CLOSE:
       
       DestroyWindow(MainWindow);
    
      break;
    
      case WM_DESTROY:
    
       PostQuitMessage(WM_QUIT);
    
      break;
    
      default: return DefWindowProc(MainWindow, Msg, wParam, lParam);
    
     }
    
     return 0;
    
    }
    What I need to know is why the program can not display all the three desktops. Only the "Default" desktop shows up.

    Thank you .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured