///
/// 創建結構體用於返回捕獲時間
///
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
///
/// 設置結構體塊容量
///
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
/// <summary>
/// 抓獲的時間
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
/// <summary>
/// 獲取鍵盤和鼠標沒有操作的時間
/// </summary>
/// <returns>用戶上次使用系統到現在的時間間隔,單位為秒</returns>
public static long GetLastInputTime()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if (!GetLastInputInfo(ref vLastInputInfo))
{
return 0;
}
else
{
long count = Environment.TickCount - (long)vLastInputInfo.dwTime;
//long icount = count / 1000;
return count;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int sunNumber=int.Parse(GetLastInputTime().ToString());
if (sunNumber >= 30000)
{
this.Close();
}
}