For security purposes in software development, especially for programs that require a username and password to log on to the server, long-term unattended operation is often considered and the program automatically jumps to the user login interface.
There are two criteria for determining whether a program has been left unattended for a long time. The first is that the mouse has not been moved for a long time, and the second is that the focus of the mouse has not been in the program for a long time (that is, the user has been operating other programs for a long time).
1. Long-term mouse immobility
You've seen a solution to this situation in other blogs [1]. Refer to this blog and add the corresponding code to App.xaml.cs (code below). In this case, restart the program if the mouse stays stationary for a long time (set 10s in this case) because you need an account password to log in.Program, keep the mouse still for a long time so that you can exit the login and restart to the login interface, thereby ensuring security.
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private DispatcherTimer mousePositionTimer; //A timer that returns the program to the login interface without operating for a long time private Point mousePosition; //Mouse position protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //Start Program mousePosition = GetMousePoint(); //Get mouse coordinates mousePositionTimer = new DispatcherTimer(); mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent); mousePositionTimer.Interval = new TimeSpan(0, 0, 10); //Detect mouse position changes every 10 seconds mousePositionTimer.Start(); } private void MousePositionTimedEvent(object sender, EventArgs e) { if (!HaveUsedTo()) { mousePositionTimer.Stop(); //Restart this program System.Windows.Forms.Application.Restart(); Application.Current.Shutdown(); } } //Determine whether the mouse is moving 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); ///Get the current screen mouse position public Point GetMousePoint() { MPoint mpt = new MPoint(); GetCursorPos(out mpt); Point p = new Point(mpt.X, mpt.Y); return p; } }
But there is a problem with this solution, as shown in Figure 1, assuming that the timer interval is 10 seconds and the mouse coordinates start at (0,0), if the mouse does not move for 10 seconds, the timer will restart the program at 10 seconds.However, when the time is 1s, we move the mouse (assuming to move to (0,1)) and then hold it still for a long time again, then when the time is 10s, the mouse coordinates are changed compared to 0s, so that no timer event will be executed. The program continues to timer for 10s, and if the mouse does not move for the next 10s, it goes toThe timer event does not respond to restarting the program until it reaches 20s.In fact, the program has undergone a program restart for 19 seconds, but the program restart requirement has not been reached if the mouse is not moving for 10 seconds.
Improved solution:
Improvements have also been made to meet the 10s mouse-fixed program restart requirement by designing a timer with an interval of 1s and adding counters that do not move the mouse to execute the program restart until 10 seconds.The implementation is as follows: every 1s to detect whether the mouse moves, if not, the counter plus 1, if the mouse moves halfway, the counter clears, to reach the counter count of 10, the mouse does not move in 10 mouse tests, so from the mouse to stop moving, to the counter to reach 10, just 10 s, to reach 10S Requirements for program restart if the mouse is not moving.The implementation code is as follows (note that this code was added to App.xaml.cs):
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private DispatcherTimer mousePositionTimer; //A timer that returns the program to the login interface without operating for a long time private Point mousePosition; //Mouse position private int checkCount = 0; //Number of times to detect mouse position protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //Start Program mousePosition = GetMousePoint(); //Get mouse coordinates mousePositionTimer = new DispatcherTimer(); mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent); mousePositionTimer.Interval = new TimeSpan(0, 0, 1); //Detect mouse position changes every 10 seconds mousePositionTimer.Start(); } private void MousePositionTimedEvent(object sender, EventArgs e) { if (!HaveUsedTo()) { checkCount++; //No mouse movement detected, checkCount + 1 if (checkCount == 10) { checkCount = 0; mousePositionTimer.Stop(); //Restart this program System.Windows.Forms.Application.Restart(); Application.Current.Shutdown(); } } else { checkCount = 0; //Mouse movement detected, recount } } //Determine whether the mouse is moving 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); ///Get the current screen mouse position public Point GetMousePoint() { MPoint mpt = new MPoint(); GetCursorPos(out mpt); Point p = new Point(mpt.X, mpt.Y); return p; } }
2. Mouse focus is not in this program for a long time
To use Activated and Deactivated events [2]: Trigger the Activated event when a top level form in the Application gains focus, that is, when the application is activated.The Deactivated event is triggered when the user switches from this application to another application.The code is as follows:
Add Activated and Deactivated events to App.xaml;
In App.xaml.cs:
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private DispatcherTimer deactivatedTimer; //Timer when focus is not on this program protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //Start Program deactivatedTimer = new DispatcherTimer(); deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick); deactivatedTimer.Interval = new TimeSpan(0, 0, 10); //If the focus is not in this program, the program restarts automatically after 10s } private void deactivatedTimer_Tick(object sender, EventArgs e) { deactivatedTimer.Stop(); //Restart this program 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(); } }
3. Combining Two Conditions
At the beginning of this paper, it has been proposed that there are two bases to judge whether a program has been left unmanned for a long time, that is, the mouse has not been moved for a long time and the focus of the mouse has not been in the program for a long time. Therefore, this paper combines the two conditions to achieve a true response to the program's long unmanned operation.The code is as follows:
App.xaml Join in Activated and Deactivated event;
In App.xaml.cs:
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private DispatcherTimer mousePositionTimer; //A timer that returns the program to the login interface without operating for a long time private Point mousePosition; //Mouse position private int checkCount = 0; //Number of times to detect mouse position private DispatcherTimer deactivatedTimer; //Timer when focus is not on this program protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //Start Program mousePosition = GetMousePoint(); //Get mouse coordinates mousePositionTimer = new DispatcherTimer(); mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent); mousePositionTimer.Interval = new TimeSpan(0, 0, 1); //Detect mouse position changes every 10 seconds mousePositionTimer.Start(); deactivatedTimer = new DispatcherTimer(); deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick); deactivatedTimer.Interval = new TimeSpan(0, 0, 10); //If the focus is not in this program, the program restarts automatically after 10s } private void MousePositionTimedEvent(object sender, EventArgs e) { if (!HaveUsedTo()) { checkCount++; //No mouse movement detected, checkCount + 1 if (checkCount == 10) { checkCount = 0; mousePositionTimer.Stop(); //Restart this program System.Windows.Forms.Application.Restart(); Application.Current.Shutdown(); } } else { checkCount = 0; //Mouse movement detected, recount } } private void deactivatedTimer_Tick(object sender, EventArgs e) { deactivatedTimer.Stop(); //Restart this program System.Windows.Forms.Application.Restart(); Application.Current.Shutdown(); } //Mouse focus back to this program private void Application_Activated(object sender, EventArgs e) { mousePositionTimer.Start(); deactivatedTimer.Stop(); } //Mouse Focus Off This Program private void Application_Deactivated(object sender, EventArgs e) { mousePositionTimer.Stop(); deactivatedTimer.Start(); } //Determine whether the mouse is moving 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); ///Get the current screen mouse position public Point GetMousePoint() { MPoint mpt = new MPoint(); GetCursorPos(out mpt); Point p = new Point(mpt.X, mpt.Y); return p; } }
Reference resources:
[1] http://blog.csdn.net/yysyangyangyangshan/article/details/8621395
[2] http://www.cnblogs.com/luluping/archive/2011/05/13/2045875.html
Code download address: http://download.csdn.net/detail/xiaoxiong345064855/6658825
Reprinted at: https://www.cnblogs.com/riasky/p/3459030.html