终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RubberbandAdorner.cs 3.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }