I'd like to be able to query some function and give it a processID or processName - It then should return true
or false
on wether that process is in the foreground or not.
我希望能够查询一些函数并给它一个processID或processName - 然后它应该返回true或false,因为该进程在前台或不在。
So i.e. the query for Firefox would return true
(because right now I'm in FireFox, typing this) and everything else should return false
.
因此,对Firefox的查询将返回true(因为现在我在FireFox中,键入此内容),其他所有内容都应返回false。
Is that even possible for every type of application (.net, java/swing, pure c++/win32-ui)?
对于每种类型的应用程序(.net,java / swing,纯c ++ / win32-ui),这是否可能?
- This question is for Windows only.
此问题仅适用于Windows。
1 个解决方案
#1
GetForegroundWindow and GetWindowThreadProcessId should let you get this information.
GetForegroundWindow和GetWindowThreadProcessId应该可以让你获得这些信息。
i.e., if you know the pid just check it against a function like this:
即,如果你知道pid只是检查它对这样的函数:
bool IsForegroundProcess(DWORD pid)
{
HWND hwnd = GetForegroundWindow();
if (hwnd == NULL) return false;
DWORD foregroundPid;
if (GetWindowThreadProcessId(hwnd, &foregroundPid) == 0) return false;
return (foregroundPid == pid);
}
This will work for any application that uses the core Win32 library at some level - this'll include Windows Forms, WPF, native Win32 applications, etc. Note this'll only work for applications running on the calling desktop and session - you can't use this to determine if another user's application is in the foreground, for instance.
这适用于在某种程度上使用核心Win32库的任何应用程序 - 这将包括Windows窗体,WPF,本机Win32应用程序等。请注意,这只适用于在调用桌面和会话上运行的应用程序 - 您可以'例如,使用它来确定另一个用户的应用程序是否在前台。
#1
GetForegroundWindow and GetWindowThreadProcessId should let you get this information.
GetForegroundWindow和GetWindowThreadProcessId应该可以让你获得这些信息。
i.e., if you know the pid just check it against a function like this:
即,如果你知道pid只是检查它对这样的函数:
bool IsForegroundProcess(DWORD pid)
{
HWND hwnd = GetForegroundWindow();
if (hwnd == NULL) return false;
DWORD foregroundPid;
if (GetWindowThreadProcessId(hwnd, &foregroundPid) == 0) return false;
return (foregroundPid == pid);
}
This will work for any application that uses the core Win32 library at some level - this'll include Windows Forms, WPF, native Win32 applications, etc. Note this'll only work for applications running on the calling desktop and session - you can't use this to determine if another user's application is in the foreground, for instance.
这适用于在某种程度上使用核心Win32库的任何应用程序 - 这将包括Windows窗体,WPF,本机Win32应用程序等。请注意,这只适用于在调用桌面和会话上运行的应用程序 - 您可以'例如,使用它来确定另一个用户的应用程序是否在前台。