SetWindowPos が外部プロセスのウィンドウを z オーダーの一番上に確実に移動できないことに問題があります。次のようにウィンドウを前面に表示できます。
NativeMethods.SetWindowPos(hwnd, new IntPtr(-1), Left, Top, Width, Height, 0x10);
NativeMethods.SetWindowPos(hwnd, new IntPtr(-2), Left, Top, Width, Height, 0x10);
ただし、常に 100% 機能するわけではありません。少し読んだ後、私はいくつかのことを見つけました。
SetWindowPos ドキュメントの状態:
To use SetWindowPos to bring a window to the top, the process that owns the window must have SetForegroundWindow permission.
MSDN の記事には、次のように記載されています
A process that is started with UIAccess rights has the following abilities:
* Set the foreground window.
AllowSetForeground メンション
The calling process must already be able to set the foreground window
マニフェストでフォアグラウンド ウィンドウを次のように設定できるように、.exe に署名し、UIAccess を有効にしました。
<requestedExecutionLevel level="highestAvailable" uiAccess="true" />
プログラムが起動し、許可を求める UAC プロンプトが表示されます。次に、UIAccess、管理者権限、および TokenElevationType をテストします。最初の 2 つは true を返し、3 番目は TokenElevationTypeFull を返します。ただし、新しいコードでも同じ問題が発生します。
私のコードは次のとおりです。
uint processid=0;
NativeMethods.GetWindowThreadProcessId(hwnd, out processid);
NativeMethods.AllowSetForegroundWindow((int)processid);
NativeMethods.SetWindowPos(hwnd, IntPtr.Zero, Left, Top, Width, Height, 0x10);
NativeMethods.RedrawWindow(hwnd, IntPtr.Zero, IntPtr.Zero, NativeMethods.RedrawWindowFlags.Erase | NativeMethods.RedrawWindowFlags.Invalidate | NativeMethods.RedrawWindowFlags.NoChildren);
NativeMethods.RedrawWindow(hwnd, IntPtr.Zero, IntPtr.Zero, NativeMethods.RedrawWindowFlags.Erase | NativeMethods.RedrawWindowFlags.Invalidate | NativeMethods.RedrawWindowFlags.UpdateNow | NativeMethods.RedrawWindowFlags.AllChildren);