zoukankan      html  css  js  c++  java
  • wpf 判断鼠标在一段时间内是否移动 分类: .NET 20120421 15:19 1408人阅读 评论(0) 收藏

    有触摸屏,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();
                        }
                    }
                }
    
            }
        }



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    BZOJ2223: [Coci 2009]PATULJCI
    BZOJ2157: 旅游
    HDU6368
    BZOJ2006: [NOI2010]超级钢琴
    BZOJ1969: [Ahoi2005]LANE 航线规划
    BZOJ1878: [SDOI2009]HH的项链
    BZOJ1798: [Ahoi2009]Seq 维护序列seq
    BZOJ1503: [NOI2004]郁闷的出纳员
    BZOJ1370: [Baltic2003]Gang团伙
    BZOJ1342: [Baltic2007]Sound静音问题
  • 原文地址:https://www.cnblogs.com/configman/p/4657591.html
Copyright © 2011-2022 走看看