我如何使用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#. 推荐答案见你如何...

我想知道如何抓住当前活动窗口的窗口标题(即一个具有焦点)使用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#.

推荐答案

见你如何能提供完整的源$ C ​​$ 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麦克林更好的正确性评论。

Edited with @Doug McClean comments for better correctness.

阅读全文

相关推荐

最新文章