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 表示复位窗口。如果您的窗口已经恢复,ShowWindow 函数将不执行任何操作。在此处查找 ShowWindow 函数的文档:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx

于 2012-09-09T17:30:03.237 回答