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)
        {
           //我是雙擊事件哦
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM