原文: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);
} /// <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 判断鼠标在一段时间内是否移动的更多相关文章

  1. 在WPF中弹出右键菜单时判断鼠标是否选中该项

      和上篇在WPF的TreeView中实现右键选定一样,这仍然是一个右键菜单的问题: 这个需求是在一个实现剪贴板的功能的时候遇到的:在弹出右键菜单时,如果菜单弹出位置在ListViewItem中时,我 ...

  2. WPF 使用鼠标拖动一个控件的实现[2018.7.15]

    原文:WPF 使用鼠标拖动一个控件的实现[2018.7.15] Q:已经把一个Shape和一个TextBlock组合起来放到了一个Grid中,现在想要实现用鼠标拖动这个Grid到任意位置的功能,如何做 ...

  3. [WPF]自定义鼠标指针

    原文:[WPF]自定义鼠标指针 [WPF]自定义鼠标指针 周银辉 看看WPF Cursor类的两个构造函数吧:  * f));            g.Flush();            g.D ...

  4. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

  5. js判断鼠标是否停止移动

    本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0: ...

  6. js判断鼠标向上滚动并浮动导航

    <div id="Jnav"> <ul class="nav"> <li><a href="#"& ...

  7. storm入门(二):关于storm中某一段时间内topN的计算入门

    刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...

  8. JS判断鼠标移入元素的方向

    最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...

  9. Matlab判断鼠标移动

    set(gcf,'WindowButtonMotionFcn',@get_cur_positon); 其中第二项是判断鼠标移动的属性参数,第三项为回调函数 et. main.m clear;clc;s ...

随机推荐

  1. 用Java对CSV文件进行读写操作

    需要jar包:javacsv-2.0.jar 读操作 // 读取csv文件的内容 public static ArrayList<String> readCsv(String filepa ...

  2. MySQL参数文件位置

    对于linux/unix: mysql --help|grep my.cnf   /etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.m ...

  3. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书

    原文:.Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书 一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户 ...

  4. CentOS7下安装Mysql失败经历--CentOS7使用yum安装和卸载Mysql过程

    起因 自己租用的BandwagonVPS上安装了个CentOS7,然后开始安装各种软件,结果yum安装MySQL发现MySQL在yum源中的Mysql不对劲,于是自己百度搜索安装方法. 终于我搜到了这 ...

  5. 走进windows编程的世界-----窗体的注冊及创建

    1   窗体注冊和创建 1.1WIN32 窗体程序创建步骤 1.WinMain入口函数的定义 2.WindowProc函数的定义 3.注冊窗体类 RegisterClass.RegisterClass ...

  6. [Grid Layout] Describe a grid layout using named grid lines

    We can use named grid lines to describe our grid layout. Let’s see how to apply this to our grid-tem ...

  7. css画电脑键盘

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 利用PS把多张psd格式的图片转换为一张PDF格式

    最近为公司做了一版电子样册,所有图片都是包含多图层高清晰的psd格式,要做成一个PDF文件的电子样册,发给客户看,面对这些零散的图片,本来打算利用在线合成:在线网址 https://smallpdf. ...

  9. 【hdu 3863】No Gambling

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...

  10. 小强的HTML5移动开发之路(38)——jqMobi插件ActionSheet

    现在在手机客户端上Action Sheet非常常见,比如微信中的分享按钮菜单,下面我们使用jqMobi实现一个Action Sheet,如下: 首先右击上面的按钮选择审查元素(我用的是Chrome浏览 ...