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 ...
随机推荐
- [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
题目链接:http://acm.swust.edu.cn/problem/1084/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- 最新的QT git代码到code.qt.io/cgit,还有planet.qt.io有许多博客
http://code.qt.io/cgit/ http://planet.qt.io/
- bootstrap base css 基本css
Headings All HTML headings, <h1> through <h6> are available. h1. Heading 1 h2. Heading 2 ...
- Cannot drop the database ‘XXX’ because it is being used for replication.
删除订阅数据库的时候出现下面的错误: Cannot drop the database ‘XXX’ because it is being used for replication. 数据库的状态为 ...
- 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 ...
- opencv鼠标绘制直线 C++版
因为需要在图片上标记直线,所以从网上找了相应的参考资料.但大多都是c风格的,于是自己就写了一个c++风格的. opencv2.4.11,win8.1,vs2013 #include <cv.h& ...
- [置顶] java的foreach循环
foreach语句是java5之后的新特征之一,在循环遍历数组.集合方面更加简洁. 使用foreach循环遍历数组和集合时,无需获得数组和集合的长度,无须根据索引来访问数组元素和集合元素,foreac ...
- sqlplus
以超级管理员登录 sqlplus sys/123 as sysdba 解锁用户 alter user xutianhao account unlock
- 我的CSS架构
写在前面 都是自己看别人的架构,自己积累下来的一些东西,这里只是阐述自己的一些观念.借此希望同行交流交流下看法,共勉. 不同架构的CSS 业务流程不同,团队配员不同.会有各种各样的CSS架构. 有的会 ...
- opencv开源库
opencv是开源库 在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib 为什么要CMake,这里我陈述自己的想法,作为一个刚使用opencv库的小白来说,有以下大概 ...