简体   繁体   English

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

[英]How do I get the title of the current active window using c#?

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

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评论更好的正确性。

如果你在谈论WPF,那么使用:

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

Use the Windows API. 使用Windows API。 Call GetForegroundWindow() . 调用GetForegroundWindow()

GetForegroundWindow() will give you a handle (named hWnd ) to the active window. GetForegroundWindow()将为活动窗口提供一个句柄(名为hWnd )。

Documentation: GetForegroundWindow function | 文档: GetForegroundWindow函数| Microsoft Docs Microsoft Docs

循环遍历Application.Current.Windows[]并找到IsActive == true那个。

Based on GetForegroundWindow function | 基于GetForegroundWindow函数| Microsoft Docs : 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;
}

It supports UTF8 characters. 它支持UTF8字符。

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;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM