protected void UpdateButtonRecords(Point mousePos, params MouseButtonState[] states) { DateTime now = DateTime.Now; int numOfPressedButtons = 0; for (int i = 0; i < Math.Min(states.Length, MouseButtonRecords.Length); i++) { double deltaArea = Math.Abs(MouseButtonRecords[i].DragRefPoint.X - mousePos.X) * Math.Abs(MouseButtonRecords[i].DragRefPoint.Y - mousePos.Y); TimeSpan deltaTime = now - MouseButtonRecords[i].TimeStamp; if (states[i] == MouseButtonState.Pressed) { if (MouseButtonRecords[i].State == MouseButtonState.Released) { MouseButtonRecords[i].DragRefPoint = mousePos; MouseButtonRecords[i].TimeStamp = now; } if (!MouseButtonRecords[i].Flags.HasFlag(DDMouseButtonFlags.Dragging)) { if ((deltaArea > ClickArea) || (deltaTime > ClickTime)) { MouseButtonRecords[i].Flags |= DDMouseButtonFlags.StartedDrag; MouseButtonRecords[i].Flags |= DDMouseButtonFlags.Dragging; } } numOfPressedButtons++; } else { if (MouseButtonRecords[i].State == MouseButtonState.Pressed) { if ((deltaArea <= ClickArea) && (deltaTime <= ClickTime)) { MouseButtonRecords[i].Flags |= DDMouseButtonFlags.Clicked; } if (MouseButtonRecords[i].Flags.HasFlag(DDMouseButtonFlags.Dragging)) { MouseButtonRecords[i].Flags |= DDMouseButtonFlags.EndedDrag; } } } MouseButtonRecords[i].State = states[i]; } if (DrawingArea != null) { if ((numOfPressedButtons > 0) && (Mouse.Captured == null)) { DrawingArea.CaptureMouse(); } else if ((numOfPressedButtons == 0) && (DrawingArea.IsMouseCaptured)) { DrawingArea.ReleaseMouseCapture(); } } }
/* * FUNCTION: DrawingArea_MouseDown * DESCRIPTION: * This method is used to determine where the mouse is clicked on the canvas. * It also determines the start point for any shape the user has chosen. * PARAMETERS: * object sender - the sender of the event * MouseButtonEventArgs - relative mouse event args based on sender * RETURNS: * None. */ private void DrawingArea_MouseDown(object sender, MouseButtonEventArgs e) { if (DrawingArea.CaptureMouse()) { startPoint = e.GetPosition(DrawingArea); switch (activeShapeType) { case ShapeType.line: newShape = true; var line = new Line { Stroke = strokeColor, StrokeThickness = activeStrokeThickness, X1 = startPoint.X, Y1 = startPoint.Y, X2 = startPoint.X, Y2 = startPoint.Y, StrokeDashArray = new DoubleCollection { 4 }, }; DrawingArea.Children.Add(line); break; case ShapeType.rectangle: newShape = true; var rectangle = new Rectangle { Stroke = strokeColor, StrokeThickness = activeStrokeThickness, Height = 0, Width = 0, StrokeDashArray = new DoubleCollection { 4 }, }; DrawingArea.Children.Add(rectangle); break; case ShapeType.ellipse: newShape = true; var ellipse = new Ellipse { Stroke = strokeColor, StrokeThickness = activeStrokeThickness, Height = 0, Width = 0, StrokeDashArray = new DoubleCollection { 4 }, }; DrawingArea.Children.Add(ellipse); break; } if (this.Title[0] != '*') { this.Title = this.Title.Insert(0, "*"); } } }