長い検索、証跡、エラーの後、コードを編集してこの問題を修正しました。
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[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")]
public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_MINIMIZE = 0xF020;
public const int SC_NEXTWINDOW = 0xF040;
public const int SW_FORCEMINIMIZE = 11;
public const int SW_HIDE = 0;
public const int SW_MAXIMIZE = 3;
public const int SW_MINIMIZE = 6;
public const int SW_RESTORE = 9;
public const int SW_SHOW = 5;
public const int SW_SHOWDEFAULT = 10;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMINNOCATIVE = 7;
public const int SW_SHOWNA = 8;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_SHOWNORMAL = 1;
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWRECTANGLE
{
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
}
/// <summary>
/// Puts all the active screens on the primairy screen.
/// </summary>
public static void putAllWindowsOnPrimairyScreen()
{
foreach (Process p in Process.GetProcesses())
{
int procesWidth, procesHeigth, defaultProcesWidth, defaultProcesHeigth;
IntPtr id = p.MainWindowHandle;
if (!string.IsNullOrEmpty(p.MainWindowTitle))
{
WINDOWRECTANGLE rect = new WINDOWRECTANGLE();
WINDOWRECTANGLE defaultrect = new WINDOWRECTANGLE();
GetWindowRect(id, ref rect);
ShowWindow(id, SW_SHOWNOACTIVATE);
GetWindowRect(id, ref defaultrect);
if (rect.Left > Screen.PrimaryScreen.Bounds.Width || rect.Left < 0 || rect.Right > Screen.PrimaryScreen.Bounds.Width || rect.Right < 0 || rect.Top > Screen.PrimaryScreen.Bounds.Height || rect.Top < 0 || rect.Bottom > Screen.PrimaryScreen.Bounds.Height || rect.Bottom < 0)
{
procesWidth = rect.Right - rect.Left;
procesHeigth = rect.Bottom - rect.Top;
defaultProcesWidth = defaultrect.Right - defaultrect.Left;
defaultProcesHeigth = defaultrect.Bottom - defaultrect.Top;
PostMessage(p.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
MoveWindow(id, 0, 0, defaultProcesWidth, defaultProcesHeigth, true);
p.Refresh();
if (procesHeigth >= Screen.PrimaryScreen.Bounds.Height || procesWidth >= Screen.PrimaryScreen.Bounds.Width) ShowWindow(id, SW_MAXIMIZE);
}
}
}
}
したがって、外部ウィンドウを他の画面に強制してそこにとどまらせることができます。このコードコンプリートをコピーして貼り付けたら、メソッドをアクティブ化するだけですputAllWindowsOnPrimairyScreen()