1

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);
4

1 に答える 1

1

あなたがしたいことは、行儀の悪いプログラムがユーザーから制御を奪うのを防ぐために Windows が用意している (複雑な) 規則に反することです。(制御とは、入力フォーカスを意味するだけでなく、表示されているものと表示されていないものを制御することも意味します。) あなたの意図はユーザーの意図と一致しているかもしれませんが、あなたが求めていることは、破壊的なプログラムがしばしばやろうとしていることと区別がつきません。

最前面以外のウィンドウに注意が必要であることをユーザーに知らせる別の方法があります。FlashWindowExたとえば、 をチェックしてください。また、通知バルーンをポップするトレイ アイコンを検討することもできます。これらは適切で効果的な解決策のようです。

于 2013-03-15T16:37:04.360 に答える