终端一体化运控平台
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.
 
 
 

93 lines
3.2 KiB

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