Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Desktop Manager for Multiple Screens

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
24 Oct 2010CPOL4 min read 36.2K   2.6K   57   3
This application will allow you to manage your Windows between multiple screens.

Introduction

During the past years, LCD screens became cheaper and cheaper so it's very common to work with multiple screens. Working with multiple screens makes life easier by giving the user bigger desktop to manage his applications view. Visual desktop applications (UNIX style or like we wrote in our previous article, Desktop_Manager.aspx) that allow you to manage virtual desktops are very helpful when you have one screen. What we trying to do is to take this one step forward to allow the user to manage his virtual desktops between multiple screens. The application is valuable for one or two screen users.

Inside the Zip File

The zip file include three folders.

Binaries

The binaries folder includes the application EXE file.

Setup

We have added a setup.exe file that allows you to install the application.

In order to uninstall it, click again on the setup file or use:

ContolPanel - > AddRemovePrograms

Code

The application code.

Operation

  1. After running the application, an icon will appear in the taskbar.

    Capture.PNG

  2. Double click on the icon or pressing CTRL+D will show the application main screen.

    applicationScreen.PNG

    The main screen shows the applications that belong to each virtual desktop. You can drag and drop the applications between the different desktops.

  3. New applications windows will be added to the Active virtual desktop. To set virtual desktop as active, you can right click on the virtual desktop name and choose Set Active. The active virtual desktop name will be colored in blue.

    setActive.png

  4. You can hide and show virtual desktop by double click on its name or by pressing CTRL+F(the virtual desktop number) for example CTRL+F1 for virtual desktop 1.
  5. You can also hide and show a single application by pressing on its name.
  6. Hidden desktop or application will be colored in grey.

Multiple Screens

While working with multiple screens, two options will be added to the virtual screen right click menu: Screen 1 and Screen 2.

mul.png

Choosing screen 1 will move all the applications that belong to this virtual desktop to screen 1 and the same for screen 2.

Comments

During the designing of the application we had to choose the policy of the application, I want to mention some of the points that floated during the work:

  • The options Screen 1 and Screen 2 are always active. Choosing virtual desktop to appear in Screen 1 doesn't mean that all of its applications appear in screen 1 because the user can change a window position manually. So the actual meaning of the screen 1 choice is to move all the application windows that appear in screen 2 to screen 1.
  • We give to the user the option to show a virtual desktop and to hide it so the user can choose to show the entire 4 virtual desktops together. Some people can find it inconvenient because if you want to change from virtual desktop 1 to virtual desktop 2, you need to do 2 actions (hide 1 and show 2) instead of 1 (toggling between the four desktops).
  • We left to the user the option to hide or show a specific window.

Using the Code

The code that I want to describe here is the one that makes the windows move from screen to screen.

  1. First, you need to import the functions GetWindowPlacement and SetWindowPlacement using the following code. In my program, I imported all the functions to the same class called WindowsApiProvider:
    C#
    [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool GetWindowPlacement
    		(IntPtr hWnd, out   WINDOWPLACEMENT lpwndpl);
    
    [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool SetWindowPlacement(IntPtr hWnd,
            [In] ref WINDOWPLACEMENT lpwndpl);
  2. After the functions are imported, I cover them with public function to prevent the using of unmanaged code, another reason to cover the functions is to make a parameters check before sending them to the original function.
    C#
    public void GetPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl)
            {
                lpwndpl.length = Marshal.SizeOf(lpwndpl);
                GetWindowPlacement(hWnd, out lpwndpl);
            }        
  3. Then, I built a class that includes all the utilities for managing windows positions called ScreenUtility, and I include there the following function:
    C#
    public WindowsApiProvider.WINDOWPLACEMENT GetWindowPlacement(IntPtr i_WindowHandle)
            {
                int showNormal = 1;
                WindowsApiProvider.WINDOWPLACEMENT place = 
    			new   WindowsApiProvider.WINDOWPLACEMENT();
                place.length = Marshal.SizeOf
    		(typeof(WindowsApiProvider.WINDOWPLACEMENT));
                place.showCmd = showNormal;
    
                m_ApiProvider.GetPlacement(i_WindowHandle, ref place);
    
                return place;
            }
  4. The last part is to write a function that moves a window by its handle to a chosen screen, the function saves the window size proportion when it moves the window between screens with different resolution.
    C#
    public void MoveWindowToScreen(IntPtr i_WindowHandle, int i_ScreenNumber)
            {
                int showMaximaized = 3;
                int showMinimized = 6;
                
    
                Screen destinationScreen = Screen.AllScreens[i_ScreenNumber];
                int currentScreen = GetWindowScreenNum(i_WindowHandle);
                Screen sourceScreen = Screen.AllScreens[currentScreen];
                WindowsApiProvider.WINDOWPLACEMENT windowPlacement = 
    				GetWindowPlacement(i_WindowHandle);
    
                int newWidth = (int)(((windowPlacement.rcNormalPosition.Width - 
    				windowPlacement.rcNormalPosition.X)));
                int newHeight = (int)((windowPlacement.rcNormalPosition.Height - 
    				windowPlacement.rcNormalPosition.Y));
            
                float percentsOfScreenY = (windowPlacement.rcNormalPosition.Y - 
    			sourceScreen.WorkingArea.Height) / 
    			(float)sourceScreen.WorkingArea.Height;
                float screensRatioWidth = (float)destinationScreen.WorkingArea.Width/ 
    			sourceScreen.WorkingArea.Width;
                float screensRatioHeigth = 
    			(float)destinationScreen.WorkingArea.Height / 
    			sourceScreen.WorkingArea.Height;
                
                windowPlacement.rcNormalPosition.X = 
    			(int)(destinationScreen.WorkingArea.X + 
    			((windowPlacement.rcNormalPosition.X - 
    			sourceScreen.WorkingArea.X)* (screensRatioWidth)));
                windowPlacement.rcNormalPosition.Y = 
    			(int)(destinationScreen.WorkingArea.Y + 
    			(int)((windowPlacement.rcNormalPosition.Y - 
    			sourceScreen.WorkingArea.Y) * (screensRatioHeigth)));
               
             
                windowPlacement.rcNormalPosition.Width = 
    			(int)(newWidth*screensRatioWidth + 
    			windowPlacement.rcNormalPosition.X);
                windowPlacement.rcNormalPosition.Height = 
    			(int)(newHeight * screensRatioHeigth + 
    			windowPlacement.rcNormalPosition.Y);
    
                if (windowPlacement.showCmd == showMaximaized)
                {
                    windowPlacement.showCmd = showMinimized;
                    m_ApiProvider.SetPlacement(i_WindowHandle, ref windowPlacement);
                    windowPlacement.showCmd = showMaximaized;
                    m_ApiProvider.SetPlacement(i_WindowHandle, ref windowPlacement);
                }
                else
                {
                    m_ApiProvider.SetPlacement(i_WindowHandle, ref windowPlacement);
                }
            }        

Future Work

  • Add different taskbar to each one of the screens
  • Change the ALT+TAB windows operation to change between applications in the same screen
*The project began as a workshop in Tel Aviv academic college. https://www.mta.ac.il/Pages/default.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
karenpayne12-Oct-10 8:03
karenpayne12-Oct-10 8:03 
Nice
GeneralMy vote of 5 Pin
Ruslan.G9-Oct-10 8:12
Ruslan.G9-Oct-10 8:12 
GeneralNice Article Pin
Ashok Gowtham9-Oct-10 7:10
Ashok Gowtham9-Oct-10 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.