使用 C# 的当前应用程序的活动时间 [英] Active time of current application using C#

查看:27
本文介绍了使用 C# 的当前应用程序的活动时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示不同 Windows 应用程序的活动时间.就像如果用户将窗口从记事本更改为浏览器,则将显示记事本活动时间.这是现在的输出,但时间不准确.我使用秒表得到输出.有什么建议吗?

I'm trying to display the active time of different windows apps. like if the user change the window from Notepad to browser then the Notepad Active time will be displayed. This is output right now but the time is not accurate. i got the output using stopwatch. Any suggesion?

    public AppInfo GetActiveWindowTitle()
    {
        var handle = GetForegroundWindow();

        string name = "";
        uint pid = 0;
        GetWindowThreadProcessId(handle, out pid);
        Process p = Process.GetProcessById((int)pid);
        var processname = p.ProcessName;
        AppInfo appInfo = new AppInfo();
        appInfo.appname = processname;
        name = GetTitle(handle);
        appInfo.apptitle = name;
        if (!st.Contains(appInfo.apptitle)) {
            if (!st.Contains(appInfo.appname))
            {
                stopWatch.Reset();
                st.Push(appInfo.apptitle);
                st.Push(appInfo.appname);
                ///appInfo.startappTime = DateTime.Now;
                stopWatch.Start();
            }
        }
        else
        {
            stopWatch.Stop();
           // appInfo.endappTime = DateTime.Now;
           // appInfo.finalAppTime = appInfo.endappTime.Subtract(appInfo.startappTime);
           finalAppTime = stopWatch.Elapsed;
            appInfo.AppTotalTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",finalAppTime.Hours, finalAppTime.Minutes, finalAppTime.Seconds,finalAppTime.Milliseconds / 10);
            string appTitle = Convert.ToString(st.Pop());
            string appName = Convert.ToString(st.Pop());
            richTextBox1.Text = richTextBox1.Text + "\nApp Title= " + appTitle + "-------App Name =" + appName + "-----------------App Time =" + appInfo.AppTotalTime;
            
        }
            return appInfo;}


AppInfo appinfo = GetActiveWindowTitle();
richTextBox1.Text = richTextBox1.Text + "\n" + appinfo.appname + "-------" + appinfo.apptitle + "-----------------" + appinfo.AppTotalTime ;

推荐答案

也许你可以通过委托WinEventDelegate来监控活动窗口.

Maybe you can monitor the active window via delegate WinEventDelegate.

这是一个获取活动时间"的简单演示.

Here is a simple demo that get "active time".

WinEventDelegate dele = null;
Stopwatch watch = new Stopwatch();

public Form1()
{
    InitializeComponent();
    dele = new WinEventDelegate(WinEventProc);
    IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
}

delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

private const uint WINEVENT_OUTOFCONTEXT = 0;
private const uint EVENT_SYSTEM_FOREGROUND = 3;

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

[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetActiveWindowTitle()
{
    const int nChars = 256;
    IntPtr handle = IntPtr.Zero;
    StringBuilder Buff = new StringBuilder(nChars);
    handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
    }
    return null;
}

string prewindow = "";

public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
    watch.Stop();

    TimeSpan timeTaken = watch.Elapsed;
    string time = "Time taken: " + timeTaken.ToString(@"m\:ss\.fff");

    if (prewindow != "")
    {
        richTextBox1.Text += prewindow + ", " + time + "\n";
    }

    prewindow = GetActiveWindowTitle();

    watch.Restart();
}

这篇关于使用 C# 的当前应用程序的活动时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆