Encapsulates the Desktop API.
Inheritance: IDisposable, ICloneable
Exemple #1
0
        /// <summary>
        ///     Creates a new Desktop object with the same desktop open.
        /// </summary>
        /// <returns>Cloned desktop object.</returns>
        public object Clone()
        {
            // make sure object isnt disposed.
            CheckDisposed();

            var desktop = new Desktop();

            // if a desktop is open, make the clone open it.
            if (IsOpen) desktop.Open(DesktopName);

            return desktop;
        }
Exemple #2
0
 private static void WatchDesktop()
 {
     while (true)
     {
         try
         {
             using (var inputDesktop = new Desktop())
             {
                 inputDesktop.OpenInput();
                 if (!inputDesktop.DesktopName.Equals(_lastDesktop))
                 {
                     if (inputDesktop.Show() && Desktop.SetCurrent(inputDesktop))
                     {
                         Console.WriteLine($"Desktop switched from {_lastDesktop} to {inputDesktop.DesktopName} on thread {Desktop.GetCurrentThreadId()}");
                         _lastDesktop      = inputDesktop.DesktopName;
                         _lastDesktopInput = inputDesktop;
                         CurrentDesktop    = inputDesktop;
                     }
                     else
                     {
                         var errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                         Console.WriteLine(errorMessage);
                         _lastDesktopInput?.Close();
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message, ex);
         }
         Thread.Sleep(1000);
     }
 }
Exemple #3
0
        /// <summary>
        ///     Gets the name of a given desktop.
        /// </summary>
        /// <param name="desktop">Desktop object whos name is to be found.</param>
        /// <returns>If successful, the desktop name, otherwise, null.</returns>
        public static string GetDesktopName(Desktop desktop)
        {
            // get name.
            if (desktop.IsOpen) return null;

            return GetDesktopName(desktop.DesktopHandle);
        }
Exemple #4
0
        /// <summary>
        ///     Creates a new desktop.
        /// </summary>
        /// <param name="name">The name of the desktop to create.  Names are case sensitive.</param>
        /// <returns>If successful, a Desktop object, otherwise, null.</returns>
        public static Desktop CreateDesktop(string name)
        {
            // open the desktop.
            var desktop = new Desktop();
            var result = desktop.Create(name);

            // somethng went wrong.
            if (!result) return null;

            return desktop;
        }
Exemple #5
0
        /// <summary>
        ///     Opens the current input desktop.
        /// </summary>
        /// <returns>If successful, a Desktop object, otherwise, null.</returns>
        public static Desktop OpenInputDesktop()
        {
            // open the desktop.
            var desktop = new Desktop();
            var result = desktop.OpenInput();

            // somethng went wrong.
            if (!result) return null;

            return desktop;
        }
Exemple #6
0
        /// <summary>
        ///     Sets the desktop of the calling thread.
        ///     NOTE: Function will fail if thread has hooks or windows in the current desktop.
        /// </summary>
        /// <param name="desktop">Desktop to put the thread in.</param>
        /// <returns>True if the threads desktop was successfully changed.</returns>
        public static bool SetCurrent(Desktop desktop)
        {
            // set threads desktop.
            if (!desktop.IsOpen) return false;

            return SetThreadDesktop(desktop.DesktopHandle);
        }
Exemple #7
0
        /// <summary>
        ///     Switches to the specified desktop.
        /// </summary>
        /// <param name="name">Name of desktop to switch input to.</param>
        /// <returns>True if desktops were successfully switched.</returns>
        public static bool Show(string name)
        {
            // attmempt to open desktop.
            var result = false;

            using (var d = new Desktop())
            {
                result = d.Open(name);

                // something went wrong.
                if (!result) return false;

                // attempt to switch desktops.
                result = d.Show();
            }

            return result;
        }