wpf 判断鼠标在一段时间内是否移动 分类: .NET 2012-04-21 15:19 1408人阅读 评论(0) 收藏
2012-04-21 15:19
375 查看
有触摸屏,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); } /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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; } }
方法二:
/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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(); } } } } }
相关文章推荐
- winform 判断鼠标在一段时间内是否移动 分类: .NET 2012-04-06 15:40 1236人阅读 评论(2) 收藏
- android 判断字符串是否为数字或中文或字母 分类: Android JAVA 2015-06-07 21:07 29人阅读 评论(0) 收藏
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字 分类: Android JAVA 2015-06-07 21:08 31人阅读 评论(0) 收藏
- WPF 文本拼写检查 分类: .NET 2012-05-23 15:30 666人阅读 评论(0) 收藏
- wpf 判断鼠标在一段时间内是否移动
- WPF ComboBox样式 分类: .NET 2015-05-11 14:06 515人阅读 评论(0) 收藏
- WPF设置控件的透明度、背景色 分类: .NET 2012-05-28 14:55 6035人阅读 评论(0) 收藏
- 判断一个数是否2的次幂 分类: python 2013-02-27 13:55 222人阅读 评论(0) 收藏
- 判断一个对象是否可迭代 的方法 分类: python 2015-03-27 12:20 82人阅读 评论(0) 收藏
- Android 判断SD卡是否存在及容量查询 分类: Android数据存储 2014-06-20 13:32 66人阅读 评论(0) 收藏
- WPF 按钮圆角 分类: .NET 2012-08-23 14:53 6722人阅读 评论(0) 收藏
- 判断网络是否连接正常 分类: urllib2 2013-12-18 18:11 347人阅读 评论(0) 收藏
- 用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放) 分类: .NET 2012-08-16 10:47 3638人阅读 评论(0) 收藏
- Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏
- C# 4.0 新特性dynamic、可选参数、命名参数等 分类: .NET 2013-07-26 11:15 414人阅读 评论(0) 收藏
- [分享]外部exe窗体嵌入winform 分类: .NET 2015-03-12 11:26 68人阅读 评论(0) 收藏
- c# WebClient上传文件到远程服务器 分类: .NET 2012-12-23 15:16 1627人阅读 评论(0) 收藏
- winhex中判断+MBR+DBR+EBR方法 分类: VC++ 2014-08-27 09:57 443人阅读 评论(0) 收藏
- 动画类型 分类: Android 2015-07-14 15:19 8人阅读 评论(0) 收藏
- 逆变与协变详解 分类: .NET 2014-02-19 10:55 336人阅读 评论(0) 收藏