本文介绍了如何测量系统空闲时间在C#中,看电影,等时不包括?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找测量系统空闲时间在Windows上的程序。我发现有很多codeS的做到这一点。例如,

I am looking for a program that measures system idle time on Windows. I have found many codes that do this. For example,

HTTP://www.$c$cproject.com/KB/ CS / GetIdleTimeWithCS.aspx

不过,我也想兼顾用户观看电影,视频,等那个时候没有输入给出,但仍系统没有闲着。

However, I also want to take into account user watching movies, videos, etc. That time no input is given, but still the system is not idle.

反正有没有做到这一点?

Is there anyway to do this?

推荐答案

此功能检测进程是否在全屏模式下的forground运行,如果发现这样返回的过程中名称:

This function detects if a process is running in fullscreen in forground, and returns name of the process if found so:

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [DllImport("user32.dll")]
    private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    private static extern bool
    GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

    private struct POINTAPI
    {
        public int x;
        public int y;
    }

    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    private struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public POINTAPI ptMinPosition;
        public POINTAPI ptMaxPosition;
        public RECT rcNormalPosition;
    }

    public string FullscreenProcess()
    {
        IntPtr foreWindow = GetForegroundWindow();

        // get the placement
        WINDOWPLACEMENT forePlacement = new WINDOWPLACEMENT();
        forePlacement.length = Marshal.SizeOf(forePlacement);
        GetWindowPlacement(foreWindow, ref forePlacement);

        if (forePlacement.rcNormalPosition.top == 0 && forePlacement.rcNormalPosition.left == 0 && forePlacement.rcNormalPosition.right == Screen.PrimaryScreen.Bounds.Width && forePlacement.rcNormalPosition.bottom == Screen.PrimaryScreen.Bounds.Height)
        {
            uint processID;
            GetWindowThreadProcessId(foreWindow, out processID);
            Process proc = Process.GetProcessById((int)processID);

            return proc.ProcessName;
        }
    return null;
    }

在此,我们只需要返回的进程名有一组流行的媒体播放器或其他进程相匹配。

After this, we just need to match the returned process name with a set of popular media players or other processes.

限制是,我们假设用户播放全屏。

Limitation is that we have assumed user plays in fullscreen.

这篇关于如何测量系统空闲时间在C#中,看电影,等时不包括?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:11