WPF 窗体中获取键盘和鼠标无操作时的超时提示
2024-10-19 23:38:57
通过调用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; } }}
最新文章
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)
- 单链表的C++实现(采用模板类)
- java类初始化的过程
- java并发编程(3):ThreadLocal
- ThreadLocal 那点事儿
- hdu-4753-Fishhead’s Little Game-记忆化搜索
- 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
- 成不了天才,但为何也没成";人材";?(转)
- 玩转Spring MVC(五)----在spring中整合log4j
- Python SyntaxError: invalid token
- spring boot 获取bean
- ajax基础知识
- pandas使用
- centos6.5下安装jdk并配置环境变量
- 【HDOJ1534】【差分约束+SPFA】
- loadrunner12.5-vugen回放脚本提示:URL=“http://www.testclass.net/js/scripts.js”的常规连接当前无套接字 (16 不足) 可用,是什么意思呢?怎么理解呢?
- HDU 1010 Tempter of the Bone (广搜+减枝)
- json demo
- diff命令的参数详解和实例
- APP界面框架初窥---标签导航
热门文章
- java 返回图片到页面
- position:relative和position:absolute的差别及使用方法
- 最简单的基于FFmpeg的AVUtil样例 (AVLog, AVOption等)
- 【37.21%】【codeforces 721B】Passwords
- Linux文件编辑命令具体整理
- Android动态修改图片颜色的实现方式分析
- Android 开发--CMakeList调用本地so文件
- 【p081】ISBN号码
- 关于android 怎样安装 assets文件下的apk
- Android多媒体开发(3)————使用Android NKD编译havlenapetr-FFMpeg-7c27aa2