我想知道如何获取在C#中打开的当前窗口的名称。 我看了看,并发现没有解决我的问题,但我希望stackoverflow可以帮助。获取当前窗口的名称打开
一个例子,我会想这样
String currentWindow = Window.Current.toString();
感谢。
我想知道如何获取在C#中打开的当前窗口的名称。 我看了看,并发现没有解决我的问题,但我希望stackoverflow可以帮助。获取当前窗口的名称打开
一个例子,我会想这样
String currentWindow = Window.Current.toString();
感谢。
这是一段代码,可以达到预期的效果,我可能很早以前从作者处获得了这样的效果,所以这可能是重复的。但非少,这里的片段:
private static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
return GetWindowText(handle, buff, nChars) > 0 ? buff.ToString() : null;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
用法示例:
string activeWindowTitle = GetActiveWindowTitle() ?? "Unknown Window";
使用platform invoke
调用GetForegroundWindow
将手柄返回到当前前台窗口。然后,您可以将句柄传递给GetWindowText
函数,该函数将返回窗口标题。
[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);
导入,然后像这样使用;
int CharacterMap = 256;
StringBuilder StringInput = new StringBuilder(CharacterMap);
IntPtr Window = GetForegroundWindow();
String WindowTitle = GetWindowText(CharactrMap,StringInput,Window) > 0 ? StringInput.ToString() null;
没有注意到上面的帖子。 Woops! D:
您是否存储对当前打开窗口的引用?此外,这是WinForms? –
目前是开放和专注的。 – Cacoon
这是什么意思?这实际上是针对您的应用程序中的“窗口”还是*任何*窗口? –