zoukankan      html  css  js  c++  java
  • WPF实现双击事件MouseDoubleClick

    本实例是基于Canvas增加双击事件

    public class RevitCanvas : Canvas
        {
            public RevitCanvas()
            {
                _oncetime = long.MaxValue;
                Focusable = true;
                this.AddHandler(Canvas.PreviewMouseDownEvent, new MouseButtonEventHandler(OnMouseDown));
    
               //这里是使用使用双击事件哦
                this.AddHandler(RevitCanvas.MouseDoubleClick, new RoutedEventHandler(OnMouseDoubleClick));
            }
    
            public static RoutedEvent MouseDoubleClick = EventManager.RegisterRoutedEvent("MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(RevitCanvas));
    
            protected void OnMouseDown(object sender, MouseButtonEventArgs e)
            {
                base.OnMouseDown(e);
    
                //双击,两次单机距离不超过4像素,时间再0.5秒以内视为双击
                Point p = e.GetPosition(this);
                long time = DateTime.Now.Ticks;
                if (Math.Abs(p.X - _oncePoint.X) < 4 && Math.Abs(p.Y - _oncePoint.Y) < 4 && (time - _oncetime < 5000000))
                {
                    this.RaiseEvent(new RoutedEventArgs(RevitCanvas.MouseDoubleClick));//此处促发双击事件
                }
                _oncetime = time;
                _oncePoint = p;
            }
    
            private void OnMouseDoubleClick(object sender, RoutedEventArgs e)
            {
               //我是双击事件哦
            }
    
            private Point _startPoint;
            private long _oncetime;
            private Point _oncePoint;
        }    

    双击事件使用:

            this.AddHandler(RevitCanvas.MouseDoubleClick, new RoutedEventHandler(OnMouseDoubleClick));
    
            private void OnMouseDoubleClick(object sender, RoutedEventArgs e)
            {
               //我是双击事件哦
            }
  • 相关阅读:
    多选按钮CheckBox
    DatePicker和DataPickerDialog以及AutoCompleteTextView的基本使用方法
    Broadcast机制(二)
    广播机制(一)
    XML文件解析
    WIFI网络操作
    SQL Server Profiler工具
    SQL Server执行计划的理解
    SQL Server常用元数据函数
    SQL Server数学函数
  • 原文地址:https://www.cnblogs.com/mqxs/p/9645015.html
Copyright © 2011-2022 走看看