1:[StructLayout(LayoutKind.Sequential)]
2: public struct LASTINPUTINOF
3: {
4: [MarshalAs(UnmanagedType.U4)]
5: public int cbSize;
6: [MarshalAs(UnmanagedType.U4)]
7: public uint dwTime;
8: }
9:
10: [DllImport("user32.dll")]
11: public static extern Boolean GetLastInputInfo(ref LASTINPUTINOF plii);
12:
13: private void MainUI_Load(object sender, EventArgs e)
14: {
15: System.Timers.Timer TimeOutCountDown = new System.Timers.Timer();
16: TimeOutCountDown.Enabled = true;
17: TimeOutCountDown.Interval = 1000; //ReadTimeOut為毫秒時間
18: TimeOutCountDown.Elapsed += new ElapsedEventHandler(TimeOutCountDown_Elapsed);
19: }
20:
21: private void TimeOutCountDown_Elapsed(Object sender, ElapsedEventArgs e)
22: {
23: LASTINPUTINOF aa = new LASTINPUTINOF();
24: aa.cbSize = Marshal.SizeOf(aa);
25: int current = Environment.TickCount; //此為系統時間
26: if (GetLastInputInfo(ref aa)) // true 為成功取得最後操作時間 false 為失敗
27: {
28: if (current - (int)aa.dwTime>5000) // 當閒置五秒後則進行以下動作
29: {
30: Resident.Visible = true;
31: Resident.ShowBalloonTip(1000);
32: GetUI();
33: }
34: }
35: }
====================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication28
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
[DllImport( " user32.dll " )]
public static extern bool GetLastInputInfo( ref LASTINPUTINFO plii);
public long getIdleTick()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if ( ! GetLastInputInfo( ref vLastInputInfo)) return 0 ;
return Environment.TickCount - ( long )vLastInputInfo.dwTime;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
this .timer1.Interval = 2000 ;
this .timer1.Enabled = true ;
}
private void timer1_Tick( object sender, EventArgs e)
{
long i = getIdleTick();
this .Text = string .Format( " jinjazz說:您已經{0}ms沒有動了 " ,i);
if (i > 10 * 1000 )
{
this .WindowState = FormWindowState.Minimized;
}
else
{
this .WindowState = FormWindowState.Normal;
}
}
}
}
====================================================
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO {
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)] public uint dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
static long GetLastInputTime()
{ LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if (!GetLastInputInfo(ref vLastInputInfo))
return 0;
return Environment.TickCount - (long)vLastInputInfo.dwTime;
}
private void Form1_Load(object sender, EventArgs e)
{ timer1.Enabled = true; }
private void timer1_Tick(object sender, EventArgs e)
{ Text = string.Format("用戶已經{0}秒沒有路過了", GetLastInputTime() / 1000); }