/// <span class="code-SummaryComment"><summary></span> /// Releases the mouse, and raises the CropImage Event /// <span class="code-SummaryComment"></summary></span> protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); if (this.IsMouseCaptured) { DisplayCanvasEventArgs args; switch (m_mode) { case DisplayCanvasModeID.FocusPoint: args = new DisplayCanvasEventArgs(DisplayCanvasEventID.FocusPointed); args.clip.X = (int)e.GetPosition(this).X; args.clip.Y = (int)e.GetPosition(this).Y; RunEvent(this, args); break; case DisplayCanvasModeID.BoxCreate: args = new DisplayCanvasEventArgs(DisplayCanvasEventID.NodeCreated); args.clip = rubber.GetClientRect(); args.HasBox = true; RunEvent(this, args); break; case DisplayCanvasModeID.BoxUpdate: if (anchors.activeAnchorID != AnchorID.None) { args = new DisplayCanvasEventArgs(DisplayCanvasEventID.NodeUpdated); args.clip = rubber.GetClientRect(); args.HasBox = true; rubber.ReInit(); // Фиксируем изменения рамки } else { // Нажали мимо рамки - потеря фокуса args = new DisplayCanvasEventArgs(DisplayCanvasEventID.FocusPointed); args.clip.X = (int)e.GetPosition(this).X; args.clip.Y = (int)e.GetPosition(this).Y; args.HasBox = false; } RunEvent(this, args); break; case DisplayCanvasModeID.Passive: case DisplayCanvasModeID.Disabled: break; default: throw new Exception("Unsupported canvas mode detected!"); } this.ReleaseMouseCapture(); } }
// Метод обновляет состояние якорей по заданным координатам // рамки выделения rubber и параметрам мышки e. Следует вызывать // из обработчика события мыши OnMouseMove(). public void Update(RubberBandManager rubber, Point mousePoint, bool IsMouseCaptured) { // Определяем радиус якорей System.Drawing.Rectangle clip = rubber.GetClientRect(); if (clip.Width < 15 || clip.Height < 15) { m_radius = 2.0; } else { m_radius = 3.0; } // Определяем координаты всех якорей Point topleft = rubber.currentTopLeftCorner; Point bottomright = rubber.currentBottomRightCorner; double widthStep = 0.5 * (bottomright.X - topleft.X); double heightStep = 0.5 * (bottomright.Y - topleft.Y); Point center = topleft; SetAnchorCenter(AnchorID.TopLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y; SetAnchorCenter(AnchorID.TopCenter, center); center.X = topleft.X + 2 * widthStep; center.Y = topleft.Y; SetAnchorCenter(AnchorID.TopRight, center); center.X = topleft.X; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleCenter, center); center.X = topleft.X + 2 * widthStep; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleRight, center); center.X = topleft.X; center.Y = topleft.Y + 2 * heightStep; SetAnchorCenter(AnchorID.BottomLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y + 2 * heightStep; SetAnchorCenter(AnchorID.BottomCenter, center); center = bottomright; SetAnchorCenter(AnchorID.BottomRight, center); // При необходимости обновляем индекс активного якоря if (!IsMouseCaptured) { // Отыскиваем ближайший к указателю мыши якорь AnchorID nearestID = AnchorID.None; double nearestDist = 1e6; foreach (KeyValuePair <AnchorID, Shape> record in m_anchors) { center = GetAnchorCenter(record.Key); double distX = Math.Abs(center.X - mousePoint.X); double distY = Math.Abs(center.Y - mousePoint.Y); double dist = Math.Sqrt(distX * distX + distY * distY); if (dist < nearestDist) { nearestDist = dist; nearestID = record.Key; } } // Сравниваем расстояние до ближайшего якоря с порогом if (nearestDist < 10) { m_activeAnchorID = nearestID; } else { m_activeAnchorID = AnchorID.None; } } // Обновляем цвета якорей в зависимости от их состояния foreach (KeyValuePair <AnchorID, Shape> record in m_anchors) { Color color; if (record.Key == m_activeAnchorID) { // Активный якорь color = IsMouseCaptured ? Colors.Blue : Colors.Red; } else { // Неактивный якорь color = m_normalColor; } record.Value.Stroke = new SolidColorBrush(color); } }