C# 获取当前活动窗口的信息
我有一个想要在后台运行的应用程序。我想获取可执行文件名称,例如 IExplorer.exe。我使用了以下代码:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main()
{
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
while (true)
{
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowText(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
Thread.Sleep(1000);
}
}
这只获取窗口标题和句柄 ID。我想获取可执行文件名称(也许还有更多信息)。
我怎样才能做到这一点?
I have an application which i want to run in the background. I want to get the executable name, for an example IExplorer.exe. I have played around with the following code:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main()
{
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
while (true)
{
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowText(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
Thread.Sleep(1000);
}
}
That only gets me the Window title, and the handle ID. I want to get the executable name (and maybe more information).
How do i achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要“GetWindowModuleFileName()”获取窗口文本
您传入了 hwnd,因此您仍然需要调用 GetForegroundWindow()
I think you want "GetWindowModuleFileName()" instead of GetWindowText
You pass in the hwnd, so you'll still need the call to GetForegroundWindow()