我正在获取所有进程的列表:
Process[] plist = Process.GetProcesses();
现在我只想让它们在桌面上可见。
换句话说,我需要一种方法来判断进程是否在窗口中运行(或在后台/隐藏)。
有什么好的方法可以检查列表中哪些进程有窗口吗?
检查窗口样式似乎可以解决问题:
public static bool hasWindowStyle(Process p)
{
IntPtr hnd = p.MainWindowHandle;
UInt32 WS_DISABLED = 0x8000000;
int GWL_STYLE = -16;
bool visible = false;
if (hnd != IntPtr.Zero)
{
UInt32 style = GetWindowLong(hnd, GWL_STYLE);
visible = ((style & WS_DISABLED) != WS_DISABLED);
}
return visible;
}
如果满足以下条件,则返回 true:
我已经做了一些测试,到目前为止,至少这似乎很好地过滤了在幕后运行的进程。
我想这只能在 Windows 上运行。