public DiagramCanvas() { this.UseLayoutRounding = true; this.SnapsToDevicePixels = true; this.Background = Brushes.White; this.visualState = Visuals.VisualState.Idle; this.visualHits = new List <DependencyObject>(); this.VisualList = new List <VisualGraphics>(); this.graphicsPolylines = new Dictionary <string, List <GraphicsPolyline> >(); }
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); Point cursorPosition = e.GetPosition(this); switch (this.visualState) { case Visuals.VisualState.Translate: { this.visualState = Visuals.VisualState.Idle; this.translateVisual = null; this.associatedPolylines = null; break; } case Visuals.VisualState.Idle: { break; } case Visuals.VisualState.Connecting: { this.visualState = Visuals.VisualState.Idle; this.firstVisual = null; this.secondVisual = null; break; } case Visuals.VisualState.Resizing: { this.visualState = Visuals.VisualState.Idle; break; } case Visuals.VisualState.InputState: { break; } default: throw new NotImplementedException(); } }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); this.previouseSelectedVisual = this.selectedVisual; Point cursorPosition = e.GetPosition(this); VisualGraphics visualHit = this.HitTestFirstVisual <ExcludedNullVisual>(cursorPosition); if (visualHit == null) { if (this.selectedVisual != null) { this.selectedVisual.IsSelected = false; this.selectedVisual.IsDrawHandle = false; this.selectedVisual.Render(); this.selectedVisual = null; } // 停止编辑状态 this.StopVisualInputState(this.previouseSelectedVisual); TextBoxEditor.Visibility = Visibility.Collapsed; // 重置到空闲状态 this.visualState = Visuals.VisualState.Idle; return; } this.ProcessSelectedVisualChanged(this.selectedVisual, visualHit); this.selectedVisual = visualHit; this.previousPosition = cursorPosition; if (e.ClickCount == 2) { // 双击图形,那么进入编辑状态 this.visualState = Visuals.VisualState.InputState; this.StartVisualInputState(this.selectedVisual); } else { this.StopVisualInputState(this.previouseSelectedVisual); #region 判断是否点击了连接点 for (int i = 0; i < visualHit.ConnectionHandles; i++) { Rect bounds = visualHit.GetConnectorBounds(i); if (bounds.Contains(cursorPosition)) { Point center = bounds.GetCenter(); ConnectionLocations location = visualHit.GetConnectorLocation(i); this.visualState = Visuals.VisualState.Connecting; GraphicsPolyline graphics = new GraphicsPolyline() { AssociatedGraphics1 = visualHit.ID, Graphics1Handle = i }; this.polyline = this.DrawVisual(graphics) as VisualPolyline; this.firstConnector = center; this.firstConnectorLocation = location; this.firstVisual = visualHit; return; } } #endregion #region 判断是否点击了Reisze点 for (int i = 0; i < visualHit.ResizeHandles; i++) { Rect bounds = visualHit.GetResizeHandleBounds(i); if (bounds.Contains(cursorPosition)) { this.resizeCenter = bounds.GetCenter(); this.resizeLocation = visualHit.Graphics.GetResizeLocation(this.resizeCenter); this.visualState = Visuals.VisualState.Resizing; return; } } #endregion #region 是平移状态 // 获取当前被移动的图形所有的连接点信息 this.associatedPolylines = this.GetAssociatedPolylines(visualHit.Graphics); Console.WriteLine("关联的折线有{0}个", this.associatedPolylines.Count); this.translateVisual = visualHit; this.visualState = Visuals.VisualState.Translate; #endregion } }