Search code examples
winapiinteroppinvoke

CascadeWindows but except for the caller window


Can I cascade all windows with this method

        [DllImport("user32.dll")]
        public static extern ushort CascadeWindows(
            HWND hwndParent,
            uint wHow,
            ref RECT lpRect,
            uint cKids,
            ref HWND lpKids
        );

but except for the current window that called the method (or a Window that I have an HWND of)?

CascadeWindows(NULL, MDITILE_ZORDER, NULL, 0, NULL); // "Cascade windows"

edit: I tried this but It only moved the main window instead of all others:

        Rectangle rect = new Rectangle(0, 0, 1740, 1010);
        var arrayRange = Process.GetProcesses()
            .Where(x => 
                !string.IsNullOrEmpty(x.MainWindowTitle) &&
                !x.MainWindowTitle.Contains("Main Window")
            )
            .Select(x => x.Handle).ToArray();

        user32.CascadeWindows(nullptr, 0, ref rect, 0, ref arrayRange); // "Cascade windows"

And also corrected the function declaration

[DllImport("user32.dll")] public static extern ushort CascadeWindows( IntPtr hwndParent, uint wHow, ref Rectangle lpRect, uint cKids, IntPtr[] lpKids);

Solution

  • Using C# this can be done like this:

            var arrayRange = Process.GetProcesses()
                .Where(x => 
                    !string.IsNullOrEmpty(x.MainWindowTitle) &&
                    !x.MainWindowTitle.Contains("MainWindow")
                );
                //.Select(x => x.Handle).ToArray();
    
            int X = 25; int Y = 25;
            foreach (var proc in arrayRange)
            {
                //DLL Import user32.dll 
                user32.MoveWindow(proc.MainWindowHandle, X, Y, 335, 335, 1);
                X += 18; Y += 18;
            }
    

    I believe that there is an option in C/C++ to do the same with process handles and MoveWindow from user32.DLL.