本實例是基於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) { //我是雙擊事件哦 }
