WPF 判断当前是否有人操作

检测当前程序是否有人操作应该再实际中还是有用的。最简单的方法时判断鼠标位置是否改变了。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 判断当前是否有人操作》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     151.58ms