我想知道如何使用C#获取当前活动窗口(即具有焦点的窗口)的窗口标题。
发布于 2010-04-16 18:43:16
如果您正在谈论WPF,则使用:
Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
发布于 2017-11-24 12:12:41
基于GetForegroundWindow function | 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;
}
它支持UTF8字符。
发布于 2009-08-27 17:47:18
循环遍历Application.Current.Windows[]
并找到带有IsActive == true
的那个。
https://stackoverflow.com/questions/115868
复制相似问题