废话不多说贴代码
引用
using System.Runtime.InteropServices;
using System.Timers;
private Timer timer1 = new Timer();
public MainWindow()
{
InitializeComponent();
}
[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;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
timer1.Elapsed += Timer1_Elapsed;
}
private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
long i = getIdleTick();
if (i > 60 * 1000)
{
MessageBox.Show("请操作");
}
}