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

时间:2022-10-24 07:31:58

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#获取当前活动窗口的窗口标题(即具有焦点的窗口)。

7 个解决方案

#1


139  

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/

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;
}

Edited with @Doug McClean comments for better correctness.

使用@Doug McClean的评论进行编辑,以获得更好的正确性。

#2


16  

If you were talking about WPF then use:

如果您正在谈论WPF,那么请使用:

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

#3


5  

Loop over Application.Current.Windows[] and find the one with IsActive = true.

在Application.Current循环。找到IsActive = true的那个。

#4


3  

Use the Windows API. Call GetForegroundWindow().

使用Windows的API。调用GetForegroundWindow()。

GetForegroundWindow() will give you a handle (named hWnd) to the active window.

GetForegroundWindow()将为活动窗口提供一个句柄(名为hWnd)。

Documentation: http://msdn.microsoft.com/en-us/library/ms633505(VS.85).aspx

文档:http://msdn.microsoft.com/en-us/library/ms633505(VS.85). aspx

#5


0  

If it happens that you need the Current Active Form from your MDI application: (MDI- Multi Document Interface).

如果您需要MDI应用程序的当前活动表单:(MDI-多文档接口)。

Form activForm;
activForm = Form.ActiveForm.ActiveMdiChild;

#6


0  

Base on https://msdn.microsoft.com/en-us/library/ms633505(VS.85).aspx

根据https://msdn.microsoft.com/en-us/library/ms633505(VS.85). aspx

[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;
}

It supports UTF8 characters.

它支持UTF8字符。

#7


-2  

you can use process class it's very easy. use this namespace

你可以使用过程类它非常简单。使用这个名称空间

using System.Diagnostics;

if you want to make a button to get active window.

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

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

#1


139  

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/

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;
}

Edited with @Doug McClean comments for better correctness.

使用@Doug McClean的评论进行编辑,以获得更好的正确性。

#2


16  

If you were talking about WPF then use:

如果您正在谈论WPF,那么请使用:

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

#3


5  

Loop over Application.Current.Windows[] and find the one with IsActive = true.

在Application.Current循环。找到IsActive = true的那个。

#4


3  

Use the Windows API. Call GetForegroundWindow().

使用Windows的API。调用GetForegroundWindow()。

GetForegroundWindow() will give you a handle (named hWnd) to the active window.

GetForegroundWindow()将为活动窗口提供一个句柄(名为hWnd)。

Documentation: http://msdn.microsoft.com/en-us/library/ms633505(VS.85).aspx

文档:http://msdn.microsoft.com/en-us/library/ms633505(VS.85). aspx

#5


0  

If it happens that you need the Current Active Form from your MDI application: (MDI- Multi Document Interface).

如果您需要MDI应用程序的当前活动表单:(MDI-多文档接口)。

Form activForm;
activForm = Form.ActiveForm.ActiveMdiChild;

#6


0  

Base on https://msdn.microsoft.com/en-us/library/ms633505(VS.85).aspx

根据https://msdn.microsoft.com/en-us/library/ms633505(VS.85). aspx

[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;
}

It supports UTF8 characters.

它支持UTF8字符。

#7


-2  

you can use process class it's very easy. use this namespace

你可以使用过程类它非常简单。使用这个名称空间

using System.Diagnostics;

if you want to make a button to get active window.

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

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