GetLastInputInfoHelper.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. //----------------------------------------------------------------
  7. //Module Name: $safeprojectname$
  8. //Purpose:
  9. //CopyRight: Muchinfo
  10. //History:
  11. //----------------------------------------------------------------
  12. //DateTime 2016/12/21 17:19:12
  13. //Author
  14. //Description Create
  15. //----------------------------------------------------------------
  16. namespace Muchinfo.MTPClient.Infrastructure.Utilities
  17. {
  18. public class GetLastInputInfoHelper
  19. {
  20. [DllImport("user32.dll")]
  21. internal static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  22. /// <summary>
  23. /// 获取鼠标键盘不活动的时间
  24. /// </summary>
  25. /// <returns>结果</returns>
  26. public static int GetLastInputTime()
  27. {
  28. LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
  29. lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
  30. lastInputInfo.dwTime = 0;
  31. int idleTime = 0;
  32. if (GetLastInputInfo(ref lastInputInfo))
  33. {
  34. idleTime = Environment.TickCount - lastInputInfo.dwTime;
  35. }
  36. return ((idleTime > 0) ? (idleTime / (1000*60)) : 0);
  37. }
  38. }
  39. [StructLayout(LayoutKind.Sequential)]
  40. internal struct LASTINPUTINFO
  41. {
  42. [MarshalAs(UnmanagedType.U4)]
  43. public int cbSize;
  44. [MarshalAs(UnmanagedType.U4)]
  45. public int dwTime;
  46. }
  47. }