【问题标题】:C# get information about current active windowC#获取当前活动窗口的信息
【发布时间】:2010-01-08 17:43:54
【问题描述】:

我有一个想要在后台运行的应用程序。我想获取可执行文件的名称,例如 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。我想获取可执行文件名称(可能还有更多信息)。

我如何做到这一点?

【问题讨论】:

标签: c# process window


【解决方案1】:

我认为你想要“GetWindowModuleFileName()”而不是 GetWindowText 你传入了 hwnd,所以你仍然需要调用 GetForegroundWindow()

【讨论】:

  • Ca 我使用一个事件来获取屏幕标题,即当用户改变屏幕焦点时它应该触发@
【解决方案2】:

快速的谷歌搜索带来了一个例子,例如C-Sharpcorner article

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
相关资源
最近更新 更多