Several times already I had the Problem that in Multimonitor mode of my laptop I undock it and some windows "stuck" to the old now non existing 2nd Screen. There are some things you can do to move it back but sometimes everything fails. So in first I made a small Autohotkey Script to move all Windows, but I wanted it done right and so I just put a small C# tool together. Opensource, feel free to use. Here a small Video how it works with my 2nd Screen attached:



For all those not like to compile a C# exe by themself here a small ready to use built version for Download:

rescreen (zip, 95.14 KB)
[Scan-Result by Virustotal]

MD5: dca794d6219deb3236d6c7083f03da47
SHA256: 7bd1c8d2d4d75c33b322b0c2c14795edc32adb587a92765b059901c8788a1000


For everybody like to improve the code please feel free to send me the improvements back, I put it on a GIST so feel free to commenmt / revision there. Some things I want to implement:
 
  • Checking if the window may already on the main screen, if so - next
  • Checking if the moved Window may be bigger than the main Screen. e.g. when 2nd screen is a 4k and main is 1080p, reduce it
  • Findinf some kind of Win10 hook to automatically run as soon as a screen is detached and only 1 is left

/* tiny tool to move all Windows to the main Screen
 * https://tcpip.wtf
 */
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace rescreen
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("rescreen - tiny window position mover.");
            Console.WriteLine("more about me at: https://tcpip.wtf");
            Process[] processlist = Process.GetProcesses();
            foreach (Process process in processlist)
            {
                // only move Windows with a set title
                if (!String.IsNullOrEmpty(process.MainWindowTitle) && !String.IsNullOrEmpty(process.ProcessName))
                {
                    Console.WriteLine("+\tmoving: " + process.MainWindowTitle);
                    // Getting the Windows Handle
                    IntPtr id = process.MainWindowHandle;
                    // Getting dimensions of Window
                    RECT rct;
                    int width = 800; int height = 600;
                    if(GetWindowRect(id, out rct))
                    {
                        width = rct.Right - rct.Left;
                        height = rct.Bottom - rct.Top;
                    } 
                    Program.MoveWindow(id, 100, 100, width, height, true);
                }
            }
        }
        [DllImport("user32.dll", SetLastError = true)] 
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }
    }
}

✉ MG// CEST

Follow Icon
Don’t miss out and subscribe by email:
Don't worry! NO Spam and FREE; Receive a summarizing email for new posts, easy to unsubscribe at any time.
← Other Blog Posts