WPF 非Control元素模拟鼠标双击MouseDoubleClick事件

复制代码
private readonly DispatcherTimer _mouseLeftTimer = new DispatcherTimer();
        private readonly DispatcherTimer _mouseRightTimer = new DispatcherTimer();


public Class()
{
            _mouseLeftTimer.Interval = TimeSpan.FromMilliseconds(MOUSE_CLICK_DELAY);
            _mouseLeftTimer.Tick += _mouseLeftTimer_Tick;
            this.PreviewMouseLeftButtonDown += AscanControlSciChart_PreviewMouseLeftButtonDown;

            _mouseRightTimer.Interval = TimeSpan.FromMilliseconds(MOUSE_CLICK_DELAY);
            _mouseRightTimer.Tick += _mouseRightTimer_Tick;
            this.PreviewMouseRightButtonDown += AscanControlSciChart_PreviewMouseRightButtonDown;
}



        private void _mouseLeftTimer_Tick(object sender, EventArgs e)
        {
            _mouseLeftTimer.Stop();
            ClickCommand?.Execute(this);
            _log.Info($"单击:{this.Title}");
        }
        private void AscanControlSciChart_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                _mouseLeftTimer.Stop();
                DoubleClickCommand?.Execute(this);
                _log.Info($"双击:{this.Title}");
            }
            else
            {
                _mouseLeftTimer.Start();
            }
        }

        private void _mouseRightTimer_Tick(object sender, EventArgs e)
        {
            _mouseRightTimer.Stop();
            RightClickCommand?.Execute(this);
            _log.Info($"右键单击:{this.Title}");
        }
        private void AscanControlSciChart_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                _mouseRightTimer.Stop();
                //右键双击
            }
            else
            {
                _mouseRightTimer.Start();
            }
        }




        #region 事件
        public ICommand ClickCommand
        {
            get { return (ICommand)GetValue(ClickCommandProperty); }
            set { SetValue(ClickCommandProperty, value); }
        }

        public static readonly DependencyProperty ClickCommandProperty =
            DependencyProperty.Register(nameof(ClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        public ICommand DoubleClickCommand
        {
            get { return (ICommand)GetValue(DoubleClickCommandProperty); }
            set { SetValue(DoubleClickCommandProperty, value); }
        }

        public static readonly DependencyProperty DoubleClickCommandProperty =
            DependencyProperty.Register(nameof(DoubleClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        public ICommand RightClickCommand
        {
            get { return (ICommand)GetValue(RightClickCommandProperty); }
            set { SetValue(RightClickCommandProperty, value); }
        }

        public static readonly DependencyProperty RightClickCommandProperty =
            DependencyProperty.Register(nameof(RightClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        #endregion
复制代码

 

posted on   lopengye  阅读(147)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示