如何使用 c# 获取当前活动窗口的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/115868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the title of the current active window using c#?
提问by d4nt
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# 获取当前活动窗口(即具有焦点的窗口)的窗口标题。
采纳答案by Jorge Ferreira
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;
}
Editedwith @Doug McClean comments for better correctness.
使用@Doug McClean 评论进行编辑以获得更好的正确性。
回答by ine
Use the Windows API. Call GetForegroundWindow()
.
使用 Windows API。打电话GetForegroundWindow()
。
GetForegroundWindow()
will give you a handle (named hWnd
) to the active window.
GetForegroundWindow()
将为您提供hWnd
活动窗口的句柄(名为)。
Documentation: GetForegroundWindow function | Microsoft Docs
回答by Jorge Ferreira
Loop over Application.Current.Windows[]
and find the one with IsActive == true
.
循环Application.Current.Windows[]
并找到带有 的那个IsActive == true
。
回答by Skvettn
If you were talking about WPF then use:
如果您在谈论 WPF,请使用:
Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
回答by B.Beshoo
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.
}
回答by Arthur Zennig
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;
回答by Mohammad Dayyan
Based on GetForegroundWindow function | Microsoft Docs:
基于GetForegroundWindow 函数| 微软文档:
[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 字符。