Using SetWindowPos in C# to move windows around

Just take out your Control.FromHandle and the form == null check. You should be able to just do:

IntPtr handle = process.MainWindowHandle;
if (handle != IntPtr.Zero)
{
    SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}

If you add SWP_NOSIZE, it will not resize the window, but will still reposition it.

If you want to effect all windows, not just the main window, of each process, you might want to consider using P/Invoke with EnumWindows instead of iterating through the Process list and using MainWindowHandle.

Leave a Comment