wpf 判断鼠标在一段时间内是否移动
有触摸屏,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();
}
}
}
}
}
wpf 判断鼠标在一段时间内是否移动的更多相关文章
- 在WPF中弹出右键菜单时判断鼠标是否选中该项
和上篇在WPF的TreeView中实现右键选定一样,这仍然是一个右键菜单的问题: 这个需求是在一个实现剪贴板的功能的时候遇到的:在弹出右键菜单时,如果菜单弹出位置在ListViewItem中时,我 ...
- WPF 使用鼠标拖动一个控件的实现[2018.7.15]
原文:WPF 使用鼠标拖动一个控件的实现[2018.7.15] Q:已经把一个Shape和一个TextBlock组合起来放到了一个Grid中,现在想要实现用鼠标拖动这个Grid到任意位置的功能,如何做 ...
- [WPF]自定义鼠标指针
原文:[WPF]自定义鼠标指针 [WPF]自定义鼠标指针 周银辉 看看WPF Cursor类的两个构造函数吧: * f)); g.Flush(); g.D ...
- JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题
1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...
- js判断鼠标是否停止移动
本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0: ...
- js判断鼠标向上滚动并浮动导航
<div id="Jnav"> <ul class="nav"> <li><a href="#"& ...
- storm入门(二):关于storm中某一段时间内topN的计算入门
刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...
- JS判断鼠标移入元素的方向
最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...
- Matlab判断鼠标移动
set(gcf,'WindowButtonMotionFcn',@get_cur_positon); 其中第二项是判断鼠标移动的属性参数,第三项为回调函数 et. main.m clear;clc;s ...
随机推荐
- 《Unix编程艺术》读书笔记(1)
<Unix编程艺术>读书笔记(1) 这两天開始阅读该书,以下是自己的体会,以及原文的摘录,尽管有些东西还无法全然吃透. 写优雅的代码来提高软件系统的透明性:(P134) Elegance ...
- Win7的ftp功能
ftp作为文件传输协议,在一些特殊情况下用这种文件传输是比较方便的,并且win7本身也支持这个功能,在控制面板--->程序-->打开或关闭Windows功能,安装即可: 然后在管理控制台中 ...
- 详解HTML的a标签(超链接标签)
原文 简书原文:https://www.jianshu.com/p/d6a2499db73b 大纲 1.什么是<a>标签 2.<a>标签的几个重要属性 3.a标签的运行机制 4 ...
- mysql 悲观锁 的运用
悲观锁: 它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持保守态度,因此,在整个数据处理过程中,将数据处于锁定状态.悲观锁的实现,往往依靠数据库提供的锁机制(也只有数 ...
- Python 标准库 —— glob
glob库是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用 windows 下的文件搜索差不多.查找文件只用到三个匹配符: "*", 匹配 0 个或多个 ...
- Error while trying to retrieve text for error ORA-12705
今天, 按照以前的学习笔记, 配置ProC 但是, 却发生了如题的错误. Google一下, 都是NLS_LANG环境变量设置有问题, 我核一下没有问题. 问题在哪? 原来是ORACLE_HOME环境 ...
- 打开cad文件的几种方法
转自原文 打开cad文件的几种方法 IWorkspaceFactory pWorkspaceFactory; IFeatureWorkspace pFeatureWorkspace; IFeature ...
- [Angular Router] Lazy loading Module with Auxiliary router
Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...
- css3-4 css3边框样式
css3-4 css3边框样式 一.总结 一句话总结: 二.css3边框样式 1.相关知识 边框属性:1.border-width2.border-style3.border-color 边框方位:1 ...
- Net Reactor 5
Net Reactor 5脱壳教程 今天别人发来一个.Net的DLL让我脱壳,第一步自然是先扔进de4dot 我这个de4dot 是集成了 Ivancito0z / TheProxy / PC- ...