如何使用 c# 获取当前活动窗口的标题? [英] How do I get the title of the current active window using c#?

查看:65
本文介绍了如何使用 c# 获取当前活动窗口的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to know how to grab the Window title of the current active window (i.e. the one that has focus) using C#.

推荐答案

在此处查看有关如何使用完整源代码执行此操作的示例:

See example on how you can do this with full source code here:

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 评论以获得更好的正确性.


Edited with @Doug McClean comments for better correctness.

这篇关于如何使用 c# 获取当前活动窗口的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆