Determines whether the current operation is performed.

Source: Internet
Author: User

It is useful to check whether the current program has been operated. The simplest way is to determine whether the mouse position has changed. The same is true in winform. Code: [csharp] public class CheckUsedManager {public static event EventHandler TimeToFinishedEvent = null; private static DispatcherTimer checkUsedTimer = new DispatcherTimer (); private static Point mousePosition = GetMousePoint (); static CheckUsedManager () {checkUsedTimer. interval = TimeSpan. fromSeconds (seconds); checkUsedTimer. tick + = new EventHandler (CheckUsedTimer_Tick); checkUsedTimer. start ();} Static void CheckUsedTimer_Tick (object sender, EventArgs e) {if (! HaveUsedTo () {if (TimeToFinishedEvent! = Null) {TimeToFinishedEvent (null, null) ;}} private static 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)] priv Ate static extern bool GetCursorPos (out MPoint mpt ); /// <summary> /// obtain the current mouse position on the screen /// </summary> /// <returns> </returns> public static Point GetMousePoint () {MPoint mpt = new MPoint (); GetCursorPos (out mpt); Point p = new Point (mpt. x, mpt. y); return p ;}} public class CheckUsedManager {public static event EventHandler TimeToFinishedEvent = null; private static DispatcherTimer checkUsedTimer = n Ew DispatcherTimer (); private static Point mousePosition = GetMousePoint (); static CheckUsedManager () {checkUsedTimer. interval = TimeSpan. fromSeconds (seconds); checkUsedTimer. tick + = new EventHandler (CheckUsedTimer_Tick); checkUsedTimer. start ();} static void CheckUsedTimer_Tick (object sender, EventArgs e) {if (! HaveUsedTo () {if (TimeToFinishedEvent! = Null) {TimeToFinishedEvent (null, null) ;}} private static 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)] priv Ate static extern bool GetCursorPos (out MPoint mpt ); /// <summary> /// obtain the current mouse position on the screen /// </summary> /// <returns> </returns> public static Point GetMousePoint () {MPoint mpt = new MPoint (); GetCursorPos (out mpt); Point p = new Point (mpt. x, mpt. y); return p ;}} then bind the event to this class. Complete Code implemented using WPF: Judgment class: [csharp] public class CheckUsedManager {public static event EventHandler TimeToFinishedEvent = null; private static DispatcherTimer checkUsedTimer = new DispatcherTimer (); private static Point mousePosition = GetMousePoint (); static CheckUsedManager () {checkUsedTimer. interval = TimeSpan. fromSeconds (ReadConfig. autoCheckUsedTime); checkUsedTimer. tick + = new EventHandler (CheckUs EdTimer_Tick); checkUsedTimer. Start ();} static void CheckUsedTimer_Tick (object sender, EventArgs e) {if (! HaveUsedTo () {if (TimeToFinishedEvent! = Null) {TimeToFinishedEvent (null, null) ;}} private static 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)] priv Ate static extern bool GetCursorPos (out MPoint mpt ); /// <summary> /// obtain the current mouse position on the screen /// </summary> /// <returns> </returns> public static Point GetMousePoint () {MPoint mpt = new MPoint (); GetCursorPos (out mpt); Point p = new Point (mpt. x, mpt. y); return p ;}} public class CheckUsedManager {public static event EventHandler TimeToFinishedEvent = null; private static DispatcherTimer checkUsedTimer = n Ew DispatcherTimer (); private static Point mousePosition = GetMousePoint (); static CheckUsedManager () {checkUsedTimer. interval = TimeSpan. fromSeconds (ReadConfig. autoCheckUsedTime); checkUsedTimer. tick + = new EventHandler (CheckUsedTimer_Tick); checkUsedTimer. start ();} static void CheckUsedTimer_Tick (object sender, EventArgs e) {if (! HaveUsedTo () {if (TimeToFinishedEvent! = Null) {TimeToFinishedEvent (null, null) ;}} private static 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)] priv Ate static extern bool GetCursorPos (out MPoint mpt ); /// <summary> /// obtain the current mouse position on the screen /// </summary> /// <returns> </returns> public static Point GetMousePoint () {MPoint mpt = new MPoint (); GetCursorPos (out mpt); Point p = new Point (mpt. x, mpt. y); return p ;}} read the time in the Configuration: [csharp] public class ReadConfig {// <summary> /// check whether the interval is used /// </summary> public static int AutoCheckUsedTime {get {int time = 10; try {string timeStr = ConfigurationManager. appSettings ["AutoCheckUsedTime"]; if (string. isNullOrEmpty (timeStr) {timeStr = "10";} time = int. parse (timeStr) ;}catch {} return time ;}}} public class ReadConfig {// <summary> /// check whether the interval is used /// </summary> public static int AutoCheckUsedTime {get {int time = 10; try {string timeStr = ConfigurationManager. appSettings ["AutoCheckUsedTime"]; If (string. isNullOrEmpty (timeStr) {timeStr = "10";} time = int. parse (timeStr);} catch {} return time ;}} configure the App. config: [csharp] <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <etettings> <add key = "AutoCheckUsedTime" value = "10"/> </appSettings> </configuration> <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <etettings> <add key = "AutoCheckUsedTime" value = "10"/> </appSettings> </configuration> interface call: [csharp] public partial class MainWindow: window {public MainWindow () {InitializeComponent (); CheckUsedManager. timeToFinishedEvent-= new EventHandler (CheckUsedManager_TimeToFinishedEvent); CheckUsedManager. timeToFinishedEvent + = new EventHandler (CheckUsedManager_TimeToFinishedEvent);} vo Id CheckUsedManager_TimeToFinishedEvent (object sender, EventArgs e) {MessageBox. Show ("unattended! ") ;}} Public partial class MainWindow: Window {public MainWindow () {InitializeComponent (); CheckUsedManager. timeToFinishedEvent-= new EventHandler (CheckUsedManager_TimeToFinishedEvent); CheckUsedManager. timeToFinishedEvent + = new EventHandler (CheckUsedManager_TimeToFinishedEvent);} void CheckUsedManager_TimeToFinishedEvent (object sender, EventArgs e) {MessageBox. show ("no one is using it! ") ;}} Result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.