原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示

通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using System.Runtime.InteropServices;namespace WpfApplication1{    /// <summary>    /// Window1.xaml 的交互逻辑    /// </summary>    public partial class Window1 : Window    {        System.Windows.Threading.DispatcherTimer timer;        public Window1()        {            InitializeComponent();            timer = new System.Windows.Threading.DispatcherTimer();            timer.Interval = new TimeSpan(0, 0, 1);            timer.Tick += new EventHandler(timer_Tick);            timer.Start();        }        void timer_Tick(object sender, EventArgs e)        {            if (GetIdleTick() / 1000 > 10)               {                MessageBox.Show("键盘或鼠标没有操作超过10秒");            }        }        [DllImport("user32.dll")]        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);        public static long GetIdleTick()        {            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);            if (!GetLastInputInfo(ref lastInputInfo)) return 0;            return Environment.TickCount - (long)lastInputInfo.dwTime;        }        [StructLayout(LayoutKind.Sequential)]        private struct LASTINPUTINFO        {            [MarshalAs(UnmanagedType.U4)]            public int cbSize;            [MarshalAs(UnmanagedType.U4)]            public uint dwTime;        }    }}

最新文章

  1. 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)
  2. 单链表的C++实现(采用模板类)
  3. java类初始化的过程
  4. java并发编程(3):ThreadLocal
  5. ThreadLocal 那点事儿
  6. hdu-4753-Fishhead’s Little Game-记忆化搜索
  7. 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
  8. 成不了天才,但为何也没成&quot;人材&quot;?(转)
  9. 玩转Spring MVC(五)----在spring中整合log4j
  10. Python SyntaxError: invalid token
  11. spring boot 获取bean
  12. ajax基础知识
  13. pandas使用
  14. centos6.5下安装jdk并配置环境变量
  15. 【HDOJ1534】【差分约束+SPFA】
  16. loadrunner12.5-vugen回放脚本提示:URL=“http://www.testclass.net/js/scripts.js”的常规连接当前无套接字 (16 不足) 可用,是什么意思呢?怎么理解呢?
  17. HDU 1010 Tempter of the Bone (广搜+减枝)
  18. json demo
  19. diff命令的参数详解和实例
  20. APP界面框架初窥---标签导航

热门文章

  1. java 返回图片到页面
  2. position:relative和position:absolute的差别及使用方法
  3. 最简单的基于FFmpeg的AVUtil样例 (AVLog, AVOption等)
  4. 【37.21%】【codeforces 721B】Passwords
  5. Linux文件编辑命令具体整理
  6. Android动态修改图片颜色的实现方式分析
  7. Android 开发--CMakeList调用本地so文件
  8. 【p081】ISBN号码
  9. 关于android 怎样安装 assets文件下的apk
  10. Android多媒体开发(3)————使用Android NKD编译havlenapetr-FFMpeg-7c27aa2