博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf 判断鼠标在一段时间内是否移动
阅读量:7190 次
发布时间:2019-06-29

本文共 4024 字,大约阅读时间需要 13 分钟。

原文:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/config_man/article/details/7484257
有触摸屏,xp系统,代码:

方法一:

class Win32    {        [StructLayout(LayoutKind.Sequential)]        public struct POINT        {            public int X;            public int Y;            public POINT(int x, int y)            {                this.X = x;                this.Y = y;            }        }        [DllImport("user32.dll", CharSet = CharSet.Auto)]        public static extern bool GetCursorPos(out POINT pt);    }    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        int x, y;        DispatcherTimer timer = new DispatcherTimer();        public MainWindow()        {            InitializeComponent();            setButtonStyle();            timer.Tick += new EventHandler(timer_Tick);            timer.Interval = new TimeSpan(0, 0, 10); //10秒后开始运行        }        void timer_Tick(object sender, EventArgs e)        {            Win32.POINT pi = new Win32.POINT();            Win32.GetCursorPos(out pi);            int _x = pi.X;            int _y = pi.Y;            if ((x + 4 == _x) && (y + 3 == _y))            {                timer.IsEnabled = false;                if (MessageBoxResult.Yes == MessageBox.Show("确定退出吗?", "友情提示", MessageBoxButton.YesNo))                {                    this.Close();                }            }        }        //鼠标左键按下时        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            //鼠标按下时获取当前鼠标坐标            x = (int)(Mouse.GetPosition(e.Source as FrameworkElement).X);            y = (int)(Mouse.GetPosition(e.Source as FrameworkElement).Y);            timer.IsEnabled = true;        }        //鼠标右键按下时        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)        {            x = -1;            y = -1;            timer.IsEnabled = false;        }   }

方法二:

///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        int x, y;        DispatcherTimer timer = new DispatcherTimer();        DispatcherTimer timer2 = new DispatcherTimer();        DateTime start;        public MainWindow()        {            InitializeComponent();            setButtonStyle();                        timer.Tick += new EventHandler(timer_Tick);            timer.Interval = new TimeSpan(0, 0, 1);            timer.Start();            timer2.Tick += new EventHandler(timer2_Tick);            timer2.Interval = new TimeSpan(0, 0, 2);            timer2.Start();        }        void timer_Tick(object sender, EventArgs e)        {            timer.Stop();            x = (int)(Mouse.GetPosition(this).X);            y = (int)(Mouse.GetPosition(this).Y);        }                bool ff = true;        void timer2_Tick(object sender, EventArgs e)        {            int _x = (int)(Mouse.GetPosition(this).X);            int _y = (int)(Mouse.GetPosition(this).Y);            if ((x == _x) && (y == _y) && ff)            {                start = DateTime.Now;                ff = false;            }            if (x != _x || y != _y)            {                x = _x;                y = _y;                start = DateTime.Now;                ff = true;            }                        TimeSpan ts = DateTime.Now.Subtract(start);                        //鼠标或键盘误动作3分钟后开始播放视频            if (ts.Minutes >= 3)            {                //MessageBox.Show("x:" + x.ToString() + ";y:" + y.ToString() + "\n _x:" + _x.ToString() + ";_y=" + _y.ToString()+"\n"+ff.ToString().ToString() + " " + ts.Hours.ToString() + " " + ts.Minutes.ToString() + " " + ts.Seconds.ToString());                //关闭所有子窗体                WindowCollection windows = Application.Current.Windows;                foreach(Window window in windows)                {                    string title = window.Title;                    if(title != "MainWindow")                    {                        window.Close();                    }                }            }        }    }

你可能感兴趣的文章
善于分解大的任务
查看>>
今晚见的几个代码
查看>>
[leetcode] Path sum路径之和
查看>>
【jquery仿datalist的一个问题,求助】——设置每行显示几列,块状DIV的解决办法...
查看>>
《Introduction to Computing Systems:From Bits and Gates to C and Beyond》读后感(一)
查看>>
apache服务器配置
查看>>
NSubstitute完全手册(十六)设置out和ref参数
查看>>
分布式消息系统Kafka初步(一) (赞)
查看>>
PostgreSQL基础知识与基本操作索引页
查看>>
初步学习pg_control文件之十一
查看>>
识骨寻踪第一季/全集Bones迅雷下载
查看>>
Unity热门插件推荐
查看>>
SpringMVC之分析RequestMappingHandlerAdapter(一)
查看>>
ABAP 中的日期处理
查看>>
html 表格
查看>>
9.4. import your signed certificate
查看>>
[LeetCode] Valid Parenthesis String 验证括号字符串
查看>>
4.3. Module
查看>>
如何用移动硬盘安装win7 系统
查看>>
《linux c编程指南》学习手记1
查看>>