WPF程序长时间无人操作
在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面。
判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标焦点长时间不在此程序中(即用户长时间在操作其他的程序)。
一、鼠标长时间不动
在其他博客中看到过针对鼠标长时间不动这种情况的解决方案【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鼠标不动则程序重启的要求。
改良后的解决方案:
经过改良,同样为了达到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程序长时间无人操作的更多相关文章
- WPF窗口长时间无人操作鼠标自动隐藏
在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...
- iOS开发笔记--如何实现程序长时间未操作退出
我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况.下面让我们看一下如何实现这种功能.我们知道iOS有一个事件循环机制,也就是大家所说的runloop.我们在对程 ...
- iOS实现程序长时间未操作退出
大部分银行客户端都有这样的需求,在用户一定时间内未操作,即认定为token失效,但未操作是任何判定的呢?我的想法是用户未进行任何touch时间,原理就是监听runloop事件.我们需要进行的操作是创建 ...
- WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z
操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...
- ASP.NET 工作流:支持长时间运行操作的 Web 应用程序
ASP.NET 工作流 支持长时间运行操作的 Web 应用程序 Michael Kennedy 代码下载位置:MSDN 代码库 在线浏览代码 本文将介绍以下内容: 独立于进程的工作流 同步和异步活 ...
- WPF:鼠标长时间无操作,窗口隐藏
//设置鼠标长时间无操作计时器 private System.Timers.Timer MouseTimerTick = new System.Timers.Timer(10000); private ...
- Web页面长时间无操作后再获取焦点时转到登录界面
今天开始讲新浪博客搬到博客园. 在工作中遇到的小问题,感觉有点意思,就记录下来吧! 该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...
- SSH连接服务器时,长时间不操作就会断开的解决方案
最近在配置服务器相关内容时候,不同的事情导致长时间不操作,页面就断开了连接,不能操作,只能关闭窗口,最后通过以下命令解决. SSH连接linux时,长时间不操作就断开的解决方案: 1.修改/etc/s ...
- web页面长时间未操作自动退出登录
var lastTime = new Date().getTime(); var currentTime = new Date().getTime(); * * ; //设置超时时间: 10分 $(f ...
随机推荐
- 【转】LINUX下一款不错的网站压力测试工具webbench
原文链接:http://blog.csdn.net/xinqingch/article/details/8618704 安装: wget http://blog.s135.com/soft/linux ...
- Tomcat 常规配置并通过zabbix 监控 jvm状态
一:jdk和tomcat基础 apache有两种方式运行php,一是使用模块,二是使用fastcgi nginx也可以通过fastcgi处理动态请求,也可以转发至tomcat tomcat监控主要是监 ...
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
安装 1准备编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 2 下载源码包(由于goog ...
- js Function 加不加new 详解
以下来自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new The new operato ...
- Xcode4.5 本地化,多语言设置
网上已有很多关于ios本地化的博客和资料,由于部分原作者使用的Xcode版本较早,4.5以后的版本已不再支持该方法,后来也没有更新,因此在此写一点学习资料分享出来.废话不多说. ios本地化主 ...
- Cannot drop the database ‘XXX’ because it is being used for replication.
删除订阅数据库的时候出现下面的错误: Cannot drop the database ‘XXX’ because it is being used for replication. 数据库的状态为 ...
- Android 多分辨率机适应
如果你有一台机器,如以下决议: 800 x 480 1024 x 600 1024 x 768 1280 x 800 1920 x 1200 2048 x 1536 总共六种类分辨率机器,假设依照dp ...
- C# ignoring letter case for if statement(Stackoverflow)
Question: I have this if statement: if (input == 'day') Console.Write({0}, dayData); When the user t ...
- sharePoint常用命令
New-SPStateServiceDatabase -Name "StateServiceDatabase" | New-SPStateServiceApplication -N ...
- Mac OSX的开机启动配置
Login Items Mac OSX的当前用户成功登录后启动的程序,该类别的启动项配置文件存放在~/Library/Preferences/com.apple.loginitems.plist,所以 ...