如何使用C#获取当前活动窗口的标题?

126

我想知道如何使用C#获取当前活动窗口(即具有焦点的窗口)的窗口标题。


2
你是在尝试确定你的应用程序中哪个窗口具有焦点,还是任何应用程序的窗口具有焦点? - Peder Rice
请参考以下链接:https://dev59.com/u3E95IYBdhLWcg3wKq2q#2428108。如果您想让按钮点击实现此功能,那么确保您的窗体不会获得焦点是值得的。 - barlop
7个回答

189

可以在这里查看完整源代码的示例:

http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetActiveWindowTitle()
{
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    IntPtr handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
    }
    return null;
}

根据@Doug McClean的评论进行修改以提高正确性。


7
不要忘记成为一个好公民。http://blogs.msdn.com/oldnewthing/archive/2007/07/27/4072156.aspx和http://blogs.msdn.com/oldnewthing/archive/2008/10/06/8969399.aspx有相关信息。 - Greg D
4
新手笔记,为了使其运行,需要使用 System.Runtime.InteropServices; 并确定放置 DllImport 和 static extern 行的位置。将它们粘贴到类中即可。 - barlop
1
@smink 如何获取特定用户的活动前台窗口(比如作为服务运行的进程)。 - tchelidze
2
您链接的网站无法访问。这里是它(可能的)网络存档:https://web.archive.org/web/20150814043810/http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/ - Piotrek
2
另外,我希望我的应用程序在前台窗口更改时得到通知。有相应的事件吗? - Piotrek
显示剩余2条评论

19

如果您在谈论WPF,则使用:

 Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);

如果整个应用程序不处于活动状态(另一个程序具有焦点),则没有窗口的 IsActive 属性将设置为 true。 - Kind Contributor
实际上那可能是错误的,在我的情况下我试图在非UI线程上访问Window数组。然而,还请参考这个链接,以防我仍然正确:https://social.msdn.microsoft.com/Forums/vstudio/en-US/654c74fe-0a88-425f-99cd-ccf97ff234bc/active-window-of-an-inactive-application?forum=wpf - Kind Contributor

10

根据GetForegroundWindow function | Microsoft Docs

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);

private string GetCaptionOfActiveWindow()
{
    var strTitle = string.Empty;
    var handle = GetForegroundWindow();
    // Obtain the length of the text   
    var intLength = GetWindowTextLength(handle) + 1;
    var stringBuilder = new StringBuilder(intLength);
    if (GetWindowText(handle, stringBuilder, intLength) > 0)
    {
        strTitle = stringBuilder.ToString();
    }
    return strTitle;
}

它支持UTF8字符。


5

循环遍历Application.Current.Windows[]并找到IsActive == true的窗口。


13
这只适用于当前 .Net 应用程序中的窗口,对吗?我认为 d4nt 想要获取桌面上当前活动窗口的标题,无论它属于哪个应用程序。 - Quagmire

3

0
如果你需要从你的 MDI 应用程序中获取当前活动窗体(MDI-多文档界面),可以使用以下代码:

Form activForm;
activForm = Form.ActiveForm.ActiveMdiChild;

-3

你可以使用Process类,它非常容易。 使用这个命名空间

using System.Diagnostics;

如果你想要创建一个按钮来获取当前活动窗口。

private void button1_Click(object sender, EventArgs e)
    {            
       Process currentp = Process.GetCurrentProcess();
       TextBox1.Text = currentp.MainWindowTitle;  //this textbox will be filled with active window.
    }

2
这是错误的。它将显示您程序的标题而不是当前活动窗口的标题。 - isHuman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接