C#获取有关当前活动窗口的信息 [英] C# get information about current active window

查看:90
本文介绍了C#获取有关当前活动窗口的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在后台运行的应用程序.我想获取可执行文件的名称,例如IExplorer.exe.我玩过以下代码:

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);
    }
}

那只会让我获得Window标题和句柄ID.我想获取可执行文件的名称(也许还有更多信息).

That only gets me the Window title, and the handle ID. I want to get the executable name (and maybe more information).

我该如何实现?

推荐答案

我认为您想要"

I think you want "GetWindowModuleFileName()" instead of GetWindowText You pass in the hwnd, so you'll still need the call to GetForegroundWindow()

这篇关于C#获取有关当前活动窗口的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆