• 注册
当前位置:1313e > 默认分类 >正文

wpf 判断鼠标在一段时间内是否移动

原文:wpf 判断鼠标在一段时间内是否移动

版权声明:本文为博主原创文章,未经博主允许不得转载。 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();}}}}}



本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录