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();
                    }
                }
            }

        }
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,可以使用鼠标事件和 Canvas.SetLeft / Canvas.SetTop 方法来实现拖动控件在 Canvas 上移动的效果。以下是一个示例: ```xaml <Canvas> <Button Content="Drag me!" Canvas.Left="50" Canvas.Top="50" MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseMove="Button_MouseMove" MouseLeftButtonUp="Button_MouseLeftButtonUp"/> </Canvas> ``` 在代码中,我们为 Button 控件注册了三个鼠标事件:MouseLeftButtonDown、MouseMove 和 MouseLeftButtonUp。这三个事件将分别在鼠标按下、移动和松开时触发。 ```csharp private bool isDragging; private Point startPoint; private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 开始拖动 isDragging = true; startPoint = e.GetPosition(null); ((UIElement)sender).CaptureMouse(); } private void Button_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { // 计算拖动距离 Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; // 移动控件 Button button = sender as Button; double left = Canvas.GetLeft(button); double top = Canvas.GetTop(button); Canvas.SetLeft(button, left - diff.X); Canvas.SetTop(button, top - diff.Y); // 更新起始点 startPoint = mousePos; } } private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // 结束拖动 isDragging = false; ((UIElement)sender).ReleaseMouseCapture(); } ``` 在这个示例中,我们首先定义了两个变量:isDragging 和 startPoint。isDragging 变量表示当前是否正在拖动控件,startPoint 变量表示拖动开始时鼠标的位置。 在 MouseLeftButtonDown 事件中,我们将 isDragging 设为 true,并记录当前鼠标的位置。然后,我们调用 CaptureMouse 方法来捕获鼠标,以确保鼠标移动事件被正确处理。 在 MouseMove 事件中,我们首先判断当前是否正在拖动。如果是,就计算出当前鼠标位置和上一次鼠标位置的差值,然后使用 Canvas.SetLeft 和 Canvas.SetTop 方法更新控件的位置。最后,我们将当前鼠标位置保存到 startPoint 变量中,以便下一次计算差值。 在 MouseLeftButtonUp 事件中,我们将 isDragging 设为 false,并调用 ReleaseMouseCapture 方法来释放鼠标捕获。 通过这种方式,我们就可以实现在 Canvas 上拖动控件移动的效果。如果需要支持多个控件同时拖动,可以使用类似的方法为每个控件注册鼠标事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值