且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查正在运行的进程是否为后台进程?

更新时间:2023-12-05 13:51:34

检查窗口样式似乎可以解决问题:

It seems like checking the window style did the trick:

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

This returns true if:

  • 该过程没有供用户输入的窗口样式

我已经进行了一些测试,到目前为止,至少这似乎可以很好地过滤掉幕后运行的流程.

I've done a little testing and as of now at least this seems to filter out the processes that are running behind the scenes pretty good.

我想这只能在Windows上使用.

I guess this only work on Windows though.