终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 lines
3.2 KiB

  1. using System.Windows;
  2. using System.Windows.Documents;
  3. using System.Windows.Input;
  4. using System.Windows.Media;
  5. namespace BPASmartClient.CustomResource.UserControls.DeCanvas
  6. {
  7. public class RubberbandAdorner : Adorner
  8. {
  9. private Point? startPoint;
  10. private Point? endPoint;
  11. private Pen rubberbandPen;
  12. private DesignerCanvas designerCanvas;
  13. public RubberbandAdorner(DesignerCanvas designerCanvas, Point? dragStartPoint)
  14. : base(designerCanvas)
  15. {
  16. this.designerCanvas = designerCanvas;
  17. this.startPoint = dragStartPoint;
  18. rubberbandPen = new Pen(Brushes.LightSlateGray, 1);
  19. rubberbandPen.DashStyle = new DashStyle(new double[] { 2 }, 1);
  20. }
  21. protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
  22. {
  23. if (e.LeftButton == MouseButtonState.Pressed)
  24. {
  25. if (!this.IsMouseCaptured)
  26. this.CaptureMouse();
  27. endPoint = e.GetPosition(this);
  28. UpdateSelection();
  29. this.InvalidateVisual();
  30. }
  31. else
  32. {
  33. if (this.IsMouseCaptured) this.ReleaseMouseCapture();
  34. }
  35. e.Handled = true;
  36. }
  37. protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
  38. {
  39. // release mouse capture
  40. if (this.IsMouseCaptured) this.ReleaseMouseCapture();
  41. // remove this adorner from adorner layer
  42. AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.designerCanvas);
  43. if (adornerLayer != null)
  44. adornerLayer.Remove(this);
  45. e.Handled = true;
  46. }
  47. protected override void OnRender(DrawingContext dc)
  48. {
  49. base.OnRender(dc);
  50. // without a background the OnMouseMove event would not be fired !
  51. // Alternative: implement a Canvas as a child of this adorner, like
  52. // the ConnectionAdorner does.
  53. dc.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize));
  54. if (this.startPoint.HasValue && this.endPoint.HasValue)
  55. dc.DrawRectangle(Brushes.Transparent, rubberbandPen, new Rect(this.startPoint.Value, this.endPoint.Value));
  56. }
  57. private void UpdateSelection()
  58. {
  59. foreach (ISelectable item in designerCanvas.SelectedItems)
  60. item.IsSelected = false;
  61. designerCanvas.SelectedItems.Clear();
  62. Rect rubberBand = new Rect(startPoint.Value, endPoint.Value);
  63. //foreach (Control item in designerCanvas.Children)
  64. //{
  65. // Rect itemRect = VisualTreeHelper.GetDescendantBounds(item);
  66. // Rect itemBounds = item.TransformToAncestor(designerCanvas).TransformBounds(itemRect);
  67. // if (rubberBand.Contains(itemBounds) && item is ISelectable)
  68. // {
  69. // ISelectable selectableItem = item as ISelectable;
  70. // selectableItem.IsSelected = true;
  71. // designerCanvas.SelectedItems.Add(selectableItem);
  72. // }
  73. //}
  74. }
  75. }
  76. }