WPF Inactivity or idle operator wpf 用户长时间无操作对程序进行处理

Related links:

 

Very important:
Monitor all key and mouse input
http://stackoverflow.com/questions/744980/hide-mouse-cursor-after-an-idle-time/745227#745227
Monitor key and mouse input for special application
http://www.codeproject.com/KB/WPF/AutologoffWPF.aspx?msg=3330342#xx3330342xx

 

Some others:
http://stackoverflow.com/questions/1111615/getting-inactivity-idle-time-in-a-wpf-application
http://www.syncfusion.com/faq/wpf/wpf_c59c.aspx
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/44406a8d-6762-421d-9fcb-b1bc23cd9f1a

 

Monitor key and mouse input for special application sample

http://www.codeproject.com/KB/WPF/AutologoffWPF/AutoLogoffInWPF.zip

 

If you are using WinForms and will only deploy on Windows machines then it's quite easy to use user32 GetLastInputInfo to handle both mouse and keyboard idling.

public static class User32Interop 
{ 
  public static TimeSpan GetLastInput() 
  { 
    var plii = new LASTINPUTINFO(); 
    plii.cbSize = (uint)Marshal.SizeOf(plii); 
 
    if (GetLastInputInfo(ref plii)) 
      return TimeSpan.FromMilliseconds(Environment.TickCount - plii.dwTime); 
    else 
      throw new Win32Exception(Marshal.GetLastWin32Error()); 
  } 
 
  [DllImport("user32.dll", SetLastError = true)] 
  static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 
 
  struct LASTINPUTINFO 
  { 
    public uint cbSize; 
    public uint dwTime; 
  } 
} 

And then in your Form

public partial class MyForm : Form 
{ 
  Timer activityTimer = new Timer(); 
  TimeSpan activityThreshold = TimeSpan.FromMinutes(2); 
  bool cursorHidden = false; 
 
  public Form1() 
  { 
    InitializeComponent(); 
 
    activityTimer.Tick += activityWorker_Tick; 
    activityTimer.Interval = 100; 
    activityTimer.Enabled = true; 
  } 
 
  void activityWorker_Tick(object sender, EventArgs e) 
  { 
    bool shouldHide = User32Interop.GetLastInput() > activityThreshold; 
    if (cursorHidden != shouldHide) 
    { 
      if (shouldHide) 
        Cursor.Hide(); 
      else 
        Cursor.Show(); 
 
      cursorHidden = shouldHide; 
    } 
  } 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值