Пример #1
0
        public static VirtualDesktop MoveToLeft(this IntPtr hWnd)
        {
            var current = VirtualDesktop.FromHwnd(hWnd);

            if (current != null)
            {
                var left = current.GetLeft();
                if (left == null)
                {
                    if (Settings.General.LoopDesktop)
                    {
                        var desktops = VirtualDesktop.GetDesktops();
                        if (desktops.Length >= 2)
                        {
                            left = desktops.Last();
                        }
                    }
                }
                if (left != null)
                {
                    VirtualDesktopHelper.MoveToDesktop(hWnd, left);
                    return(left);
                }
            }

            SystemSounds.Asterisk.Play();
            return(null);
        }
Пример #2
0
        private bool MoveWindow(IntPtr hwnd, int index)
        {
            if (hwnd != IntPtr.Zero)
            {
                for (int i = 0; i < activeWindows.Length; ++i)
                {
                    if (activeWindows[i] == hwnd)
                    {
                        activeWindows[i] = IntPtr.Zero;
                    }
                }

                movingWindow = hwnd;

                Debug.WriteLine("Move " + hwnd + " from " + Current + " to " + index);

                try {
                    VirtualDesktopHelper.MoveToDesktop(hwnd, VirtualDesktop.FromId(desktopIdLookup[index]));
                    activeWindows[index] = hwnd;
                    if (hwnd != WinAPI.GetForegroundWindow())
                    {
                        WinAPI.SetForegroundWindow(hwnd);
                    }
                    Current = index;
                } catch {
                    return(false);
                }
                return(true);
            }
            return(false);
        }
Пример #3
0
        public static VirtualDesktop MoveToRight(this IntPtr hWnd, IVdmHelper helper)
        {
            var current = VirtualDesktop.FromHwnd(hWnd);

            if (current != null)
            {
                var right = current.GetRight();
                if (right == null)
                {
                    if (Settings.General.LoopDesktop)
                    {
                        var desktops = VirtualDesktop.GetDesktops();
                        if (desktops.Length >= 2)
                        {
                            right = desktops.First();
                        }
                    }
                }
                if (right != null)
                {
                    if (VirtualDesktopHelper.MoveToDesktop(hWnd, right) ||
                        helper.MoveWindowToDesktop(hWnd, right.Id))
                    {
                        return(right);
                    }
                }
            }

            SystemSounds.Asterisk.Play();
            return(null);
        }
Пример #4
0
        public void MoveToDesktop(int desktopNumber)
        {
            try
            {
                //Create addtional desktops if necessary
                VirtualDesktop[] Desktops = VirtualDesktop.GetDesktops();
                if (Desktops.Count() < desktopNumber)
                {
                    int diff = Math.Abs(Desktops.Count() - desktopNumber);
                    for (int x = 1; x <= diff; x++)
                    {
                        VirtualDesktop.Create();
                    }
                }

                VirtualDesktop current = VirtualDesktop.Current;

                int i = GetDesktopNumber(current.Id);
                if (i == desktopNumber)
                {
                    return;
                }
                else
                {
                    int diff = Math.Abs(i - desktopNumber);
                    if (i < desktopNumber)
                    {
                        for (int z = 1; z <= diff; z++)
                        {
                            current = current.GetRight();
                        }
                    }
                    else
                    {
                        for (int z = 1; z <= diff; z++)
                        {
                            current = current.GetLeft();
                        }
                    }
                    VirtualDesktopHelper.MoveToDesktop(hWnd, current);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "An error occured while moving the specified window. See additional details below." +
                    Environment.NewLine +
                    Environment.NewLine +
                    ex.Message +
                    Environment.NewLine +
                    ex.Source +
                    "::" +
                    ex.TargetSite.Name);
            }
        }
Пример #5
0
        public static VirtualDesktop MoveTo(this IntPtr hWnd, VirtualDesktop target)
        {
            if (target == null)
            {
                SystemSounds.Asterisk.Play();
                return(null);
            }

            VirtualDesktopHelper.MoveToDesktop(hWnd, target);
            return(target);
        }
Пример #6
0
 private VirtualDesktop launchProcessOnDesktop(Process proc, int n)
 {
     createMaxDesktops(n);
     while (proc.MainWindowHandle.ToInt64() == 0)
     {
         // spawning the process can be slow, wait for a few ms until the process has created a main window.
         // TODO: exit this while loop and do not call .MoveToDesktop if the process doesn't yeild a main window handle in a reasonable timeframe.
         Thread.Sleep(0);
     }
     VirtualDesktopHelper.MoveToDesktop(proc.MainWindowHandle, getDesktopFromIndex(n));
     return(getDesktopFromIndex(n));
 }
Пример #7
0
        public override Task <BoolResult> IsHWndOnDesktop(WindowHandle request, ServerCallContext context)
        {
            var hWnd = new IntPtr(request.HWnd);

            Logger.Debug(() => $"Received request: IsHWndOnDesktop({ hWnd.ToHexString() })");

            var result = VirtualDesktopHelper.IsCurrentVirtualDesktop(hWnd);

            return(Task.FromResult(new BoolResult {
                Result = result
            }));
        }
Пример #8
0
        public static VirtualDesktop MoveToNew(this IntPtr hWnd)
        {
            var newone = VirtualDesktop.Create();

            if (newone != null)
            {
                VirtualDesktopHelper.MoveToDesktop(hWnd, newone);
                return(newone);
            }

            SystemSounds.Asterisk.Play();
            return(null);
        }
 private void ToggleWindowSticky(IntPtr hwnd)
 {
     try {
         if (WindowsDesktop.Interop.ComObjects.GetVirtualDesktopPinnedApps() == null)
         {
             WinAPI.SetWindowLongPtr(hwnd, WinAPI.GWL_EXSTYLE,
                                     WinAPI.GetWindowLongPtr(hwnd, WinAPI.GWL_EXSTYLE).XOR(WinAPI.WS_EX_TOOLWINDOW));
         }
         else
         {
             VirtualDesktopHelper.TogglePinWindow(hwnd);
         }
     } catch { }
 }
Пример #10
0
 public Window MoveToDesktop(VirtualDesktop desktop)
 {
     if (DesktopNumber != -1)
     {
         var info = AppInfo.FromWindow(this);
         if (IsPinnedApplication)
         {
             VirtualDesktop.UnpinApplication(info.Id);
             AppModel.PinnedApps.Remove(info);
         }
         AppModel.PinnedWindows.Remove(this);
         VirtualDesktopHelper.MoveToDesktop(Handle, desktop);
     }
     return(this);
 }
Пример #11
0
        public static VirtualDesktop MoveToNew(this IntPtr hWnd, IVdmHelper helper)
        {
            var newone = VirtualDesktop.Create();

            if (newone != null)
            {
                if (VirtualDesktopHelper.MoveToDesktop(hWnd, newone) ||
                    helper.MoveWindowToDesktop(hWnd, newone.Id))
                {
                    return(newone);
                }
            }

            SystemSounds.Asterisk.Play();
            return(null);
        }
Пример #12
0
        private async void CreateNewAndMove(object sender, RoutedEventArgs e)
        {
            var desktop = VirtualDesktop.Create();

            if (this.ThisWindowMenu.IsChecked ?? false)
            {
                this.MoveToDesktop(desktop);
            }
            else
            {
                await Task.Delay(_delay);

                VirtualDesktopHelper.MoveToDesktop(GetForegroundWindow(), desktop);
            }

            desktop.Switch();
        }
Пример #13
0
 /// <summary>
 /// Allow the GridSetters to be hidden or restore when changing virtual desktop.
 /// </summary>
 private static void ManageGridSetters()
 {
     foreach (Process process in Process.GetProcesses())
     {
         if (process.ProcessName.ToLower().StartsWith("gridsetter"))
         {
             var windowHandle = process.MainWindowHandle;
             if (VirtualDesktopHelper.IsCurrentVirtualDesktop(windowHandle))
             {
                 ShowWindow(windowHandle, (int)ShowWindowCommandEnum.Maximize);
             }
             else
             {
                 ShowWindow(windowHandle, (int)ShowWindowCommandEnum.Minimize);
             }
         }
     }
 }
Пример #14
0
 private void OpenSettings()
 {
     if (SettingsForm?.Visible ?? false)
     {
         if (VirtualDesktop.Current.GetNumber() !=
             VirtualDesktop.FromHwnd(SettingsForm.Handle).GetNumber())
         {
             VirtualDesktopHelper.MoveToDesktop(SettingsForm.Handle,
                                                VirtualDesktop.Current);
         }
         User32.SetForegroundWindow(SettingsForm.Handle);
     }
     else
     {
         SettingsForm = new SettingsForm();
         SettingsForm.ShowDialog();
         SettingsForm = null;
     }
 }
Пример #15
0
        private void processCommand(string command)
        {
            char[] chars = command.ToCharArray();
            switch (chars[0])
            {
            case 'd':
            {
                int index = int.Parse(command.Substring(1)) - 1;
                VirtualDesktop.GetDesktops()[index].Switch();
                break;
            }

            case 'm':
            {
                int index = int.Parse(command.Substring(1)) - 1;
                VirtualDesktopHelper.MoveToDesktop(GetForegroundWindow(), VirtualDesktop.GetDesktops()[index]);
                break;
            }
            }
        }
        private void MoveWindow(IntPtr hwnd, int index)
        {
            if (hwnd != IntPtr.Zero)
            {
                for (int i = 0; i < activeWindows.Length; ++i)
                {
                    if (activeWindows[i] == hwnd)
                    {
                        activeWindows[i] = IntPtr.Zero;
                    }
                }

                movingWindow = hwnd;

                Debug.WriteLine("Move " + hwnd + " from " + Current + " to " + index);
                if (!VirtualDesktopHelper.MoveToDesktop(hwnd, desktops[index]))
                {
                    VDMHelper.MoveWindowToDesktop(hwnd, desktops[index].Id);
                }
            }
        }
Пример #17
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            VirtualDesktopProvider.Default.Initialize().Wait();
            string TargetArgs = "";

            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].Contains(" "))
                {
                    TargetArgs += "\"" + args[i] + "\" ";
                }
                else
                {
                    TargetArgs += args[i] + " ";
                }
            }
            Process targetProcess = Process.Start(args[0], TargetArgs);

            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                if (targetProcess.MainWindowHandle.ToInt64() != 0)
                {
                    VirtualDesktop[] desktops = VirtualDesktop.GetDesktops();
                    if (desktops.Length < 2)
                    {
                        desktops.Append(VirtualDesktop.Create());
                    }
                    VirtualDesktopHelper.MoveToDesktop(targetProcess.MainWindowHandle, desktops[1]);
                    //ShowWindow(targetProcess.MainWindowHandle, 1);
                    return;
                }
            }
            targetProcess.Kill();
        }
Пример #18
0
        private async void SwitchLeftAndMove(object sender, RoutedEventArgs e)
        {
            var left = this.GetCurrentDesktop().GetLeft();

            if (left == null)
            {
                return;
            }

            if (this.ThisWindowMenu.IsChecked ?? false)
            {
                this.MoveToDesktop(left);
            }
            else
            {
                await Task.Delay(_delay);

                VirtualDesktopHelper.MoveToDesktop(GetForegroundWindow(), left);
            }

            left.Switch();
        }
Пример #19
0
        private static void SwapDesktops(VirtualDesktop source, VirtualDesktop target)
        {
            var srcWindows = new List <IntPtr>();
            var dstWindows = new List <IntPtr>();

            foreach (var window in GetAllWindows())
            {
                var desktop = VirtualDesktop.FromHwnd(window);
                if (desktop == source)
                {
                    srcWindows.Add(window);
                }
                else if (desktop == target)
                {
                    dstWindows.Add(window);
                }
            }

            foreach (var window in srcWindows)
            {
                VirtualDesktopHelper.MoveToDesktop(window, target);
            }
            foreach (var window in dstWindows)
            {
                VirtualDesktopHelper.MoveToDesktop(window, source);
            }

            // Swap the names as well
            int sourceNumber = DesktopHelper.GetNumber(source);
            int targetNumber = DesktopHelper.GetNumber(target);
            var sourceName   = SettingsHelper.GetDesktopName(sourceNumber);
            var targetName   = SettingsHelper.GetDesktopName(targetNumber);

            SettingsHelper.SetDesktopName(sourceNumber, targetName);
            SettingsHelper.SetDesktopName(targetNumber, sourceName);
        }
        private bool MoveWindow(IntPtr hwnd, int index)
        {
            if (hwnd != IntPtr.Zero)
            {
                for (int i = 0; i < activeWindows.Length; ++i)
                {
                    if (activeWindows[i] == hwnd)
                    {
                        activeWindows[i] = IntPtr.Zero;
                    }
                }

                movingWindow = hwnd;

                Debug.WriteLine("Move " + hwnd + " from " + Current + " to " + index);

                try {
                    VirtualDesktopHelper.MoveToDesktop(hwnd, desktops[index]);
                    activeWindows[index] = hwnd;
                    if (hwnd != WinAPI.GetForegroundWindow())
                    {
                        WinAPI.SetForegroundWindow(hwnd);
                    }
                    Current = index;
                } catch (Exception ex) {
                    MessageBox.Show("Exception caught",
                                    "Warning",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                    Console.WriteLine(ex.ToString());
                    return(false);
                }
                return(true);
            }
            return(false);
        }
Пример #21
0
        private void ApplicationStart(object sender, StartupEventArgs e)
        {
            try {
                if (!VirtualDesktop.IsSupported)
                {
                    throw new NotSupportedException("Virtual Desktops are not supported on this system.");
                }

                string[] clArgs = Environment.GetCommandLineArgs();

                int    exeNameLength = Regex.Match(Environment.CommandLine, "^(?:\".+?\"|\\S+)").Value.Length;
                string commandline   = string.Concat(Environment.CommandLine.Skip(exeNameLength + 1));

                Dictionary <string, string> args = new Dictionary <string, string> {
                    ["noswitch"] = "false"
                };

                foreach (string arg in clArgs)
                {
                    string[] kv = arg.Split(':');
                    args[kv[0]] = kv.Length > 1 ? string.Concat(string.Concat(kv.Skip(1)).Split()[0]) : "";
                }

                if (commandline.Contains("create"))
                {
                    //just create desktops.
                    if (args.ContainsKey("create") && int.TryParse(args["create"], out int n))
                    {
                        while (n > VirtualDesktop.GetDesktops().Length)
                        {
                            VirtualDesktop.Create();
                        }
                    }
                    else
                    {
                        VirtualDesktop.Create();
                    }
                }
                else if (commandline.Contains("run:"))
                {
                    //launch program:
                    int    startIndex = commandline.IndexOf("run:") + 4;
                    string appString  = commandline.Substring(startIndex);

                    string appPath = Regex.Match(appString, "^(?:\".+?\"|\\S+)").Value;
                    string appArgs = appString.Length >= appPath.Length + 1 ? appString.Substring(appPath.Length + 1) : "";
                    appPath = appPath.Trim('"');

                    VirtualDesktop targetDesktop;

                    if (args.ContainsKey("on") && int.TryParse(args["on"], out int i))
                    {
                        //launch on desktop i
                        VirtualDesktop[] desktops = VirtualDesktop.GetDesktops();

                        while (i > VirtualDesktop.GetDesktops().Length)
                        {
                            VirtualDesktop.Create();
                        }

                        targetDesktop = VirtualDesktop.GetDesktops()[Math.Max(0, --i)];
                    }
                    else
                    {
                        //launch on new desktop
                        targetDesktop = VirtualDesktop.Create();
                    }

                    if (bool.TryParse(args["noswitch"], out bool noswitch) && !noswitch)
                    {
                        targetDesktop.Switch();
                    }

                    Process proc = Process.Start(appPath, appArgs);

                    if (noswitch)
                    {
                        for (int backoff = 1; proc.MainWindowHandle.ToInt64() == 0 && backoff <= 0x1000; backoff <<= 1)
                        {
                            Thread.Sleep(backoff);
                        }

                        if (proc.MainWindowHandle.ToInt64() != 0)
                        {
                            VirtualDesktopHelper.MoveToDesktop(proc.MainWindowHandle, targetDesktop);
                        }
                    }
                }
                else
                {
                    //wip: gui mode
                    MessageBox.Show(
                        @"Usage:
vdesk create[:n]
vdesk [on:<n>] [noswitch:{true|false}] <run:command> [args]
",
                        "VDesk Usage");
                }
            } catch (FileNotFoundException ex) {
                MessageBox.Show($"{ex.Message}:{Environment.NewLine}'{ex.FileName}'", "VDesk", MessageBoxButton.OK, MessageBoxImage.Hand);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "VDesk");
            } finally {
                Current.Shutdown();
            }
        }
Пример #22
0
        public void MoveToDesktop(int desktopNumber)
        {
            try
            {
                if (Program.IsExcludedWindow(this.Caption))
                {
                    return;
                }
                //Create addtional desktops if necessary
                VirtualDesktop[] Desktops = VirtualDesktop.GetDesktops();
                if (Desktops.Count() < desktopNumber)
                {
                    int diff = Math.Abs(Desktops.Count() - desktopNumber);
                    for (int x = 1; x <= diff; x++)
                    {
                        VirtualDesktop.Create();
                    }
                }

                VirtualDesktop current = VirtualDesktop.Current;

                int i = GetDesktopNumber(current.Id);
                if (i == desktopNumber)
                {
                    VirtualDesktopHelper.MoveToDesktop(hWnd, current);
                    return;
                }
                else
                {
                    int diff = Math.Abs(i - desktopNumber);
                    if (i < desktopNumber)
                    {
                        for (int z = 1; z <= diff; z++)
                        {
                            current = current.GetRight();
                        }
                    }
                    else
                    {
                        for (int z = 1; z <= diff; z++)
                        {
                            current = current.GetLeft();
                        }
                    }
                    VirtualDesktopHelper.MoveToDesktop(hWnd, current);
                    Program.MoveCount++;
                }
            }
            catch (Exception ex)
            {
                if (this.Caption != "")
                {
                }

                Log.LogEvent("Exception", "",
                             "Window Handle: " + this.Handle.ToString() + Environment.NewLine +
                             "Window Caption: " + this.Caption + Environment.NewLine +
                             "Application: " + this.ApplicationName,
                             "Window",
                             ex);
            }
        }
Пример #23
0
        internal static IVirtualDesktop GetVirtualDesktop(Guid id)
        {
            VirtualDesktopHelper.ThrowIfNotSupported();

            return(_virtualDesktops.GetOrAdd(id, x => VirtualDesktopManagerInternal.FindDesktop(ref x)));
        }
Пример #24
0
        private void Run(object sender, RoutedEventArgs e)
        {
            var allWindows = getAllWindows();
            var desktops   = VirtualDesktop.GetDesktops();


            var screens = new List <VDSScreen>()
            {
                new VDSScreen(0, 1, 6, 12, new List <global::App> {
                    new global::App("brave", 1, POSITION_FULLSCREEN),
                    new global::App("code", 3, POSITION_RIGHT),
                    new global::App("chrome", 3, POSITION_LEFT),
                    new global::App("foobar2000", 2, POSITION_RIGHT),
                    new global::App("spotify", 2, POSITION_LEFT),
                    new global::App("thunderbird", 1, POSITION_FULLSCREEN),
                }),
                new VDSScreen(1, 1.5, 6, 12, new List <global::App> {
                    new global::App("discord", 3, new Position(1, 0, 11, 2), true),
                    new global::App("jamm", 3, new Position(9, 3, 11, 5), true),
                    new global::App("telegram", 3, new Position(2, 3, 9, 5), true),
                    new global::App("twist", 3, new Position(0, 0, 10, 2), true),
                    new global::App("slack", 3, new Position(0, 3, 7, 5), true),
                }),
            };

            // TODO: create desktops if there aren't enough

            foreach (var screen in screens)
            {
                screen.Apps.ForEach(app =>
                {
                    var regex   = new Regex(app.NameRegexp.ToLower());
                    var windows = (allWindows.Where(w => regex.IsMatch(w.ProcessName.ToLower()))).ToList();
                    windows.ForEach(window =>
                    {
                        try
                        {
                            var desktop = desktops[app.TargetDesktop - 1];
                            VirtualDesktopHelper.MoveToDesktop(window.hWnd, desktop);

                            System.Drawing.Rectangle workingArea = Screen.AllScreens[screen.Index].WorkingArea;
                            int DESKTOP_WIDTH  = workingArea.Width;
                            int DESKTOP_HEIGHT = workingArea.Height;

                            if (app.Position == null)
                            {
                                return;
                            }

                            var WindowXPosition = (int)(workingArea.Left) + (DESKTOP_WIDTH / COLUMNS) * app.Position.X1;
                            var WindowYPosition = (int)(workingArea.Top) + (DESKTOP_HEIGHT / ROWS) * app.Position.Y1;
                            var windowWidth     = (int)(DESKTOP_WIDTH / COLUMNS) * (app.Position.X2 - app.Position.X1 + 1);
                            var windowHeight    = (int)(DESKTOP_HEIGHT / ROWS) * (app.Position.Y2 - app.Position.Y1 + 1);

                            MoveWindow(window.hWnd, WindowXPosition, WindowYPosition, windowWidth, windowHeight, true);

                            if (app.Pin)
                            {
                                VirtualDesktop.PinWindow(window.hWnd);
                            }
                        }
                        catch (System.Exception e)
                        {
                            // TODO: add debug logging
                            if (!e.Message.Contains("Element not found") && !e.Message.Contains("The group or resource"))
                            {
                                System.Console.WriteLine("====================");
                                System.Console.WriteLine(e.Message);
                                System.Console.WriteLine("====================");
                            }
                        }
                    });
                });
            }
        }
Пример #25
0
 public static void TogglePin(this IntPtr hWnd)
 {
     VirtualDesktopHelper.TogglePinWindow(hWnd);
 }
Пример #26
0
 /// <summary>
 /// Move this window to specified virtual desktop.
 /// </summary>
 public static void MoveToDesktop(this Window window, VirtualDesktop.VirtualDesktop virtualDesktop)
 {
     VirtualDesktopHelper.MoveToDesktop(window.GetHandle(), virtualDesktop);
 }
Пример #27
0
 public static void Unpin(this IntPtr hWnd)
 {
     VirtualDesktopHelper.UnpinWindow(hWnd);
 }
Пример #28
0
 private void MoveWindowToDesktop(IntPtr window, VirtualDesktop desktop)
 {
     VirtualDesktopHelper.MoveToDesktop(window, desktop);
 }
Пример #29
0
 /// <summary>
 /// Determines whether the window is located over the virtual desktop that current displayed.
 /// </summary>
 public static bool IsCurrentVirtualDesktop(this Window window)
 {
     return(VirtualDesktopHelper.IsCurrentVirtualDesktop(window.GetHandle()));
 }