首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

WPF-006:判断现阶段是否有人操作

2013-03-01 
WPF-006:判断当前是否有人操作检测当前程序是否有人操作应该再实际中还是有用的。最简单的方法时判断鼠标位

WPF-006:判断当前是否有人操作

检测当前程序是否有人操作应该再实际中还是有用的。最简单的方法时判断鼠标位置是否改变了。winform中也是一样。直接看代码:

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(秒数);            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)]        private static extern bool GetCursorPos(out MPoint mpt);        /// <summary>        /// 获取当前屏幕鼠标位置        /// </summary>        /// <returns></returns>        public static Point GetMousePoint()        {            MPoint mpt = new MPoint();            GetCursorPos(out mpt);            Point p = new Point(mpt.X, mpt.Y);            return p;        }    }

 然后绑定到该类的事件即可。如下用WPF实现的完整代码:


 判断的类:

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(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)]        private static extern bool GetCursorPos(out MPoint mpt);        /// <summary>        /// 获取当前屏幕鼠标位置        /// </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 ReadConfig    {        /// <summary>        /// 检测是否使用间隔时间        /// </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;            }        }    }

配置App.config:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <appSettings>    <add key="AutoCheckUsedTime" value="10"/>  </appSettings></configuration>

界面调用:

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("无人使用!");        }    }

结果:
WPF-006:判断现阶段是否有人操作
工程代码下载:
http://download.csdn.net/detail/yysyangyangyangshan/5095178

热点排行