PoshCode Archive  Artifact [fff0569b00]

Artifact fff0569b00b6ebb689dbbd5b0b689a9de4c860da5f0336e68818b149a4e940be:

  • File idle-time.ps1 — part of check-in [0a37c9e748] at 2018-06-10 13:43:07 on branch trunk — See http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/ms646272(v=vs.85).aspx (user: greg zakharov size: 3635)

# encoding: ascii
# api: csharp
# title: idle time
# description: See http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/ms646272(v=vs.85).aspx
# version: 0.1
# type: class
# author: greg zakharov
# license: CC0
# x-poshcode-id: 4566
# x-archived: 2016-12-13T12:51:07
# x-published: 2013-10-28T07:28:00
#
#
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Idle Time")]
[assembly: AssemblyVersion("2.0.0.0")]

namespace IdleTime {
  internal sealed class AssemblyInfo {
    internal Type t;
    internal AssemblyInfo() { t = typeof(frmMain); }
    
    internal string Title {
      get {
        Object[] a = t.Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
        return ((AssemblyTitleAttribute)a[0]).Title;
      }
    }
  }
  
  internal static class WinAPI {
    [StructLayout(LayoutKind.Sequential)]
    internal struct PLASTINPUTINFO {
      public uint cbSize;
      public uint dwTime;
    }
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool GetLastInputInfo(ref PLASTINPUTINFO plii);
    
    internal static DateTime LastInput {
      get {
        PLASTINPUTINFO lii = new PLASTINPUTINFO();
        lii.cbSize = (uint)Marshal.SizeOf(typeof(PLASTINPUTINFO));
        GetLastInputInfo(ref lii);
        DateTime bTime = DateTime.Now.AddMilliseconds(-Environment.TickCount);
        DateTime lTime = bTime.AddMilliseconds(lii.dwTime);
        
        return lTime;
      }
    }
    
    internal static TimeSpan IdleTime {
      get {
        return DateTime.Now.Subtract(LastInput);
      }
    }
  }
  
  internal sealed class frmMain : Form {
    public frmMain() {
      InitializeComponent();
      this.Text = (new AssemblyInfo()).Title;
    }

    private Label lblLabel1;
    private Label lblLabel2;
    private Timer tmrUpdate;
    
    private void InitializeComponent() {
      this.lblLabel1 = new Label();
      this.lblLabel2 = new Label();
      this.tmrUpdate = new Timer();
      //
      //lblLabel1
      //
      this.lblLabel1.Location = new Point(13, 13);
      this.lblLabel1.Size = new Size(200, 19);
      this.lblLabel1.Text = "Idle Time: " + WinAPI.IdleTime.ToString();
      //
      //lblLabel2
      //
      this.lblLabel2.Location = new Point(13, 33);
      this.lblLabel2.Size = new Size(200, 19);
      this.lblLabel2.Text = "Last Input: " + WinAPI.LastInput.ToString();
      //
      //tmrUpdate
      //
      this.tmrUpdate.Enabled = true;
      this.tmrUpdate.Interval = 1000;
      this.tmrUpdate.Tick += new EventHandler(tmrUpdate_Tick);
      //
      //frmMain
      //
      this.ClientSize = new Size(300, 70);
      this.Controls.AddRange(new Control[] {this.lblLabel1, this.lblLabel2});
      this.FormBorderStyle = FormBorderStyle.FixedSingle;
      this.MaximizeBox = false;
      this.StartPosition = FormStartPosition.CenterScreen;
    }
    
    private void tmrUpdate_Tick(object sender, EventArgs e) {
      lblLabel1.Text = lblLabel2.Text = String.Empty;
      lblLabel1.Text = "Idle Time: " + WinAPI.IdleTime.ToString();
      lblLabel2.Text = "Last Input: " + WinAPI.LastInput.ToString();
    }
  }
  
  internal sealed class Program {
    [STAThread]
    static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new frmMain());
    }
  }
}