是否有可能获得当前活动的应用程序的名称有可能、应用程序、名称

2023-09-03 15:13:59 作者:去他奶奶的天长地久

用户可以通过Alt + Tab键或单击其在任务栏图标切换活动的应用程序。是否有可能获得当前活动的应用程序的名称(或其他独特的特点)?

User can switch active application by Alt+Tab or by clicking on their icons in TaskBar. Is it possible to get the name (or other unique characteristic) of current active application?

我想编写一个程序,收集应用程序使用情况的统计数据。

I want to write a program which collects statistic of the applications usage.

推荐答案

在Windows API有一个名为函数GetForegroundWindow().您将需要使用P / Invoke来调用到的Win32 API。在P / Invoke的wiki有更多信息中的C#用户。

The Windows API has a function called GetForegroundWindow(). You will need to use P/Invoke to call into the Win32 API. The P/Invoke wiki has more info for C# users.

请参阅this页它获取当前应用程序的标题(名称)的一个例子。

See this page for an example which gets the caption (name) of the current application.