`
lovnet
  • 浏览: 6706759 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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;
  }
 }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics