在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面。

判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标焦点长时间不在此程序中(即用户长时间在操作其他的程序)。

一、鼠标长时间不动

在其他博客中看到过针对鼠标长时间不动这种情况的解决方案【1】,参考此博客,将相应的代码加入到App.xaml.cs(代码如下),本文实现的是鼠标长时间不动(本例中设置10s不动)则重启该程序(因为对于需要账号密码登录的程序,鼠标长时间不动这样就能退出登录,并重新启动到登录界面,从而保证了安全性)。

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private DispatcherTimer mousePositionTimer; //长时间不操作该程序退回到登录界面的计时器
private Point mousePosition; //鼠标的位置 protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); //启动程序
mousePosition = GetMousePoint(); //获取鼠标坐标 mousePositionTimer = new DispatcherTimer();
mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
mousePositionTimer.Interval = new TimeSpan(0, 0, 10); //每隔10秒检测一次鼠标位置是否变动
mousePositionTimer.Start();
} private void MousePositionTimedEvent(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
mousePositionTimer.Stop();
//重新启动此程序
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
}
} //判断鼠标是否移动
private bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
} [StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y; public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
} [DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt); /// 获取当前屏幕鼠标位置
public Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}

但是这个解决方案有个问题,如图1所示,假设计时器的间隔时间为10s,鼠标坐标的起始点为(0,0),如果鼠标10s内不动,则到10s时计时器会执行重启程序操作。但是,当时间在1s时,我们移动了鼠标(假设移动到(0,1)),然后再次鼠标长时间不动,则当时间到10s时鼠标相比0s时刻,鼠标的坐标是变动了的,从而不会执行计时器事件,程序继续10s计时,如果接下来10s鼠标也不移动,则到达20s时计时器事件才会响应重启程序的操作。这样程序实际上是经历了19s才进行程序重启,没达到10s鼠标不动则程序重启的要求。

图 1

改良后的解决方案:

经过改良,同样为了达到10s鼠标不动则程序重启的要求,我们设计了计时器的间隔时间为1s,并添加鼠标没移动的计数器,计数器达到10才执行程序重启。实现是这样的:每隔1s检测鼠标是否移动,如果不移动则计数器加1,如果中途鼠标移动,则计数器清零,要达到计数器计数为10,则要10次鼠标检测中鼠标都不移动,这样从鼠标停止移动,到计数器达到10,刚好是10s,能够达到10s鼠标不动则程序重启的要求。具体实现代码如下(注意此代码是添加在App.xaml.cs中的):

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private DispatcherTimer mousePositionTimer; //长时间不操作该程序退回到登录界面的计时器
private Point mousePosition; //鼠标的位置
private int checkCount = 0; //检测鼠标位置的次数 protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); //启动程序
mousePosition = GetMousePoint(); //获取鼠标坐标 mousePositionTimer = new DispatcherTimer();
mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
mousePositionTimer.Interval = new TimeSpan(0, 0, 1); //每隔10秒检测一次鼠标位置是否变动
mousePositionTimer.Start();
} private void MousePositionTimedEvent(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
checkCount++; //检测到鼠标没移动,checkCount + 1
if (checkCount == 10)
{
checkCount = 0;
mousePositionTimer.Stop();
//重新启动此程序
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
} }
else
{
checkCount = 0; //检测到鼠标移动,重新计数
}
} //判断鼠标是否移动
private bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
} [StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y; public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
} [DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt); /// 获取当前屏幕鼠标位置
public Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}

二、鼠标焦点长时间不在此程序中

要用到Activated和Deactivated事件【2】:当Application中的一个top level窗体获得焦点时触发Activated事件,也就是应用程序被激活时。当用户从本应用程序切换到其他应用程序时触发Deactivated事件。具体代码如下:

App.xaml中加入Activated和Deactivated事件;

App.xaml.cs中:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private DispatcherTimer deactivatedTimer; //当焦点不在此程序上时计时器 protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); //启动程序
deactivatedTimer = new DispatcherTimer();
deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick);
deactivatedTimer.Interval = new TimeSpan(0, 0, 10); //如果焦点不在此程序中时,过10s程序自动重启
} private void deactivatedTimer_Tick(object sender, EventArgs e)
{
deactivatedTimer.Stop();
//重新启动此程序
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
} private void Application_Activated(object sender, EventArgs e)
{
deactivatedTimer.Stop();
} private void Application_Deactivated(object sender, EventArgs e)
{
deactivatedTimer.Start();
} }

三、综合两种情况

本文开始时已经提出,判断程序是否长时间无人操作有两个依据,即鼠标长时间不动和鼠标焦点长时间不在此程序中,于是本文综合了两种情况,做到了真实实现程序长时间无人操作的响应。具体代码如下:

App.xaml
中加入
Activated和
Deactivated事件;

App.xaml.cs中:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private DispatcherTimer mousePositionTimer; //长时间不操作该程序退回到登录界面的计时器
private Point mousePosition; //鼠标的位置
private int checkCount = 0; //检测鼠标位置的次数 private DispatcherTimer deactivatedTimer; //当焦点不在此程序上时计时器 protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); //启动程序
mousePosition = GetMousePoint(); //获取鼠标坐标 mousePositionTimer = new DispatcherTimer();
mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
mousePositionTimer.Interval = new TimeSpan(0, 0, 1); //每隔10秒检测一次鼠标位置是否变动
mousePositionTimer.Start(); deactivatedTimer = new DispatcherTimer();
deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick);
deactivatedTimer.Interval = new TimeSpan(0, 0, 10); //如果焦点不在此程序中时,过10s程序自动重启
} private void MousePositionTimedEvent(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
checkCount++; //检测到鼠标没移动,checkCount + 1
if (checkCount == 10)
{
checkCount = 0;
mousePositionTimer.Stop();
//重新启动此程序
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
} }
else
{
checkCount = 0; //检测到鼠标移动,重新计数
}
} private void deactivatedTimer_Tick(object sender, EventArgs e)
{
deactivatedTimer.Stop();
//重新启动此程序
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
} //鼠标焦点回到此程序
private void Application_Activated(object sender, EventArgs e)
{
mousePositionTimer.Start();
deactivatedTimer.Stop();
} //鼠标焦点离开此程序
private void Application_Deactivated(object sender, EventArgs e)
{
mousePositionTimer.Stop();
deactivatedTimer.Start();
} //判断鼠标是否移动
private bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
} [StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y; public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
} [DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt); /// 获取当前屏幕鼠标位置
public Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
} }

参考:

【1】      http://blog.csdn.net/yysyangyangyangshan/article/details/8621395

【2】      http://www.cnblogs.com/luluping/archive/2011/05/13/2045875.html

代码下载地址:http://download.csdn.net/detail/xiaoxiong345064855/6658825

WPF程序长时间无人操作的更多相关文章

  1. WPF窗口长时间无人操作鼠标自动隐藏

    在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...

  2. iOS开发笔记--如何实现程序长时间未操作退出

    我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况.下面让我们看一下如何实现这种功能.我们知道iOS有一个事件循环机制,也就是大家所说的runloop.我们在对程 ...

  3. iOS实现程序长时间未操作退出

    大部分银行客户端都有这样的需求,在用户一定时间内未操作,即认定为token失效,但未操作是任何判定的呢?我的想法是用户未进行任何touch时间,原理就是监听runloop事件.我们需要进行的操作是创建 ...

  4. WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z

    操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...

  5. ASP.NET 工作流:支持长时间运行操作的 Web 应用程序

    ASP.NET 工作流 支持长时间运行操作的 Web 应用程序 Michael Kennedy   代码下载位置:MSDN 代码库 在线浏览代码 本文将介绍以下内容: 独立于进程的工作流 同步和异步活 ...

  6. WPF:鼠标长时间无操作,窗口隐藏

    //设置鼠标长时间无操作计时器 private System.Timers.Timer MouseTimerTick = new System.Timers.Timer(10000); private ...

  7. Web页面长时间无操作后再获取焦点时转到登录界面

    今天开始讲新浪博客搬到博客园.        在工作中遇到的小问题,感觉有点意思,就记录下来吧!        该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...

  8. SSH连接服务器时,长时间不操作就会断开的解决方案

    最近在配置服务器相关内容时候,不同的事情导致长时间不操作,页面就断开了连接,不能操作,只能关闭窗口,最后通过以下命令解决. SSH连接linux时,长时间不操作就断开的解决方案: 1.修改/etc/s ...

  9. web页面长时间未操作自动退出登录

    var lastTime = new Date().getTime(); var currentTime = new Date().getTime(); * * ; //设置超时时间: 10分 $(f ...

随机推荐

  1. The Hungarian algorithm Template

    The Hungarian algorithm with The adjacency matrix : 计算最大匹配问题 int n1, n2, m, ans; int res[MAXN]; bool ...

  2. iOS5.1下emoji表情显示方框的解决办法

    在iOS5.1的部分设备上,emoji表情无法正常显示.我测试了一下,iOS5.1(9B176 for iPhone 4)无法正常显示emoji,全部是方框iOS5.1(9B179 for iPhon ...

  3. VC命令行编译参数介绍

    CL.exe是控制Microsoft C和C++编译器与链接器的32位工具.编译器产生通用对象文件格式(COFF)对象(.obj)文件.链接器产生可执行文件(.exe)或动态链接库文件(DLL). 注 ...

  4. 使用webservice实现App与服务器端数据交互

    What? webservice曾经认为是解决异构系统间整合的最佳解决方案,不依赖于第三方任何系统的支持(不需要部署RDBMS服务器),大家只需要按照官方的规范,即可完成相互之间的数据交互. Why? ...

  5. Pison geeker

    Pison on scriptogr.am Pison Abraham Lincoln: "Nearly all men can stand adversity, but if you wa ...

  6. [置顶] CSS+DIV总结

         HTML在Web飞速发展的过程中起着重要作用,有着重要地位.HTML初衷是为了表达标签(<p>.<table>)的内容信息.同时文档布局由浏览器来完成,不使用任何格式 ...

  7. MUI跳转页面传值

    1.打开新的页面.通过 mui.openWindow 打开页面extras参数传递参数 mui.openWindow({ id: "yingshou-" + newid, url: ...

  8. Multiple bindings were found on the class path(转)

    Multiple bindings were found on the class path SLF4J API is designed to bind with one and only one u ...

  9. java字符操作获取汉字的拼音以及其它经常使用工具

    公司需求年年有,今年有点小特殊,哈哈. 忽然加了个需求,说要实现汉字转拼音查询. 在努力下写出来了,如今分享一下吧!.! /** * 汉字转拼音缩写 * * @param str * 要转换的汉字字符 ...

  10. 使用struts2和poi导出excel文档

    poi眼下应该是比較流行的操作excel的工具了.这几天做了个struts2和poi结合使用来实现导出excel的功能.个人认为还是比較有用的.代码阅读起来也非常easy.下来就来分享下我的心得 1  ...