2

開いている他のすべてのアプリケーションのウィンドウの上部に表示されるポップアップウィンドウとしてウィンドウフォームを表示したいのですが、Focusメソッドを使用しましたが、機能しませんでした。だから私は試しました:

using System.Diagnostics;
using System.Runtime.InteropServices;

// Sets the window to be foreground
[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);

// Activate or minimize a window
[DllImportAttribute("User32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;

private void ActivateApplication(string briefAppName)
{
    Process[] procList = Process.GetProcessesByName(briefAppName);

    if (procList.Length > 0)
    {
        ShowWindow(procList[0].MainWindowHandle, SW_RESTORE);
        SetForegroundWindow(procList[0].MainWindowHandle);
    }
}

SO Hereに関する前の質問で述べたように、 私はそれを使用できませんでした。正解のポスターには「Basically, call ShowWindow() then SetForegroundWindow().」と書かれていましたが、これらのメソッドのパラメータはわかりませんでした。

ShowWindow();とSetForegroundWindow()に正確に何を渡す必要が ありますか。メソッド?? 何か助けはありますか?

4

1 に答える 1

2

これが私の解決策です:

private void ActivateApplication (string briefAppName) 
    {
        Process[] p=Process.GetProcessesByName (briefAppName);
        if (p.Length>0)
        {
            this.TopMost=true;
            ShowWindow (p[0].MainWindowHandle, 9);
            this.TopMost=false;
            this.Activate ();
        }
    }

.Activate()を使用してフォームにフォーカスを合わせ、TopMostを使用してフォームのAlways-on-top状態を変更します。

9はResotreウィンドウを意味します。ウィンドウがすでに復元されている場合、ShowWindow関数は何もしません。ShowWindow関数のドキュメントについてはこちらをご覧ください:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx

于 2012-09-09T17:30:03.237 に答える