Search code examples
c#processborderredrawwindow-position

Force external process to redraw borders after SetWindowPos


I am just trying to see if I can make a program that will sit along the left side of my monitor.

By doing so I am using a BackgroundWorker to loop through all of the users process (ones with a MainWindowTitle) and using SetWindowPos to move and resize them based upon my sidebar.

This all works fine except it causes the border to not draw (I guess that is a way to explain it).

I have attached 2 images and as you can see the borders don't seem to draw (and for Visual Studio it doesn't resize based upon the application BorderStyle)

This is the code I have so far:

foreach (Process p in Process.GetProcesses())
{
    if (p.MainWindowTitle == "") continue;

    if (p.MainWindowTitle.ToLower().Contains("studio"))
    {
        IntPtr i = p.MainWindowHandle;

        RECT r;
        GetWindowRect(i, out r);

        if (r.Left <= -1608)
            SetWindowPos(i, HWND.Top, Screen.AllScreens[1].Bounds.Left + 200, Screen.AllScreens[1].Bounds.Top, Screen.AllScreens[1].Bounds.Width - 200, Screen.AllScreens[1].Bounds.Height, SetWindowPosFlags.SWP_NOACTIVATE);
    }
}

As you can see I am just trying to resize and reposition any (just Visual Studio at the moment) window on my second monitor (to the left of my first using a hackish kind of check :D)

Visual Studio

Chrome


Solution

  • I think you are misinterpreting what you see there: the borders do not draw, because (before you moved them) the windows where MAXIMIZED. Maximized windows typically don't have a border in Windows.

    The solution is to first bring them back to normal state, before moving them to the desired coordinate, i.e.

    ShowWindow(i, ShowWindowCommands.Normal);
    

    That said, you'll still have to deal with multi-window processes and a myriad of fun little challenges. Enjoy the ride :-).