【问题标题】:To show hidden windows form at the top of the current opened application在当前打开的应用程序顶部显示隐藏的窗口窗体
【发布时间】:2012-09-02 16:17:57
【问题描述】:

我想将 windows 窗体显示为一个弹出窗口,出现在所有打开的其他应用程序窗口的顶部 我使用了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();方法?? 有什么帮助吗?

【问题讨论】:

    标签: c# winforms forms show-hide


    【解决方案1】:

    这是我的解决方案:

    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

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      相关资源
      最近更新 更多