我想将窗口窗体显示为弹出窗口,该窗口出现在所有打开的其他应用程序窗口的顶部
我已经使用了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 的问题中提到的
但我无法使用它。
正确答案海报说“基本上,先调用 ShowWindow(),然后调用 SetForegroundWindow()。
”,但我不知道这些方法的参数是什么
我到底应该将什么传递给 ShowWindow();
和 SetForegroundWindow();方法??
有什么帮助吗?
最佳答案
这是我的解决方案:
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
关于c# - 在当前打开的应用程序顶部显示隐藏的窗口窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340837/