终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

156 řádky
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Documents;
  7. using System.Windows.Input;
  8. using System.Windows.Markup;
  9. using System.Xml;
  10. namespace BPASmartClient.CustomResource.UserControls.DeCanvas
  11. {
  12. public class DesignerCanvas :Canvas
  13. {
  14. // start point of the rubberband drag operation
  15. private Point? rubberbandSelectionStartPoint = null;
  16. // keep track of selected items
  17. private List<ISelectable> selectedItems;
  18. public List<ISelectable> SelectedItems
  19. {
  20. get
  21. {
  22. if (selectedItems == null)
  23. selectedItems = new List<ISelectable>();
  24. return selectedItems;
  25. }
  26. set
  27. {
  28. selectedItems = value;
  29. }
  30. }
  31. public DesignerCanvas()
  32. {
  33. this.AllowDrop = true;
  34. }
  35. protected override void OnMouseDown(MouseButtonEventArgs e)
  36. {
  37. base.OnMouseDown(e);
  38. if (e.Source == this)
  39. {
  40. // in case that this click is the start for a
  41. // drag operation we cache the start point
  42. this.rubberbandSelectionStartPoint = new Point?(e.GetPosition(this));
  43. // if you click directly on the canvas all
  44. // selected items are 'de-selected'
  45. foreach (ISelectable item in SelectedItems)
  46. item.IsSelected = false;
  47. selectedItems.Clear();
  48. e.Handled = true;
  49. }
  50. }
  51. protected override void OnMouseMove(MouseEventArgs e)
  52. {
  53. base.OnMouseMove(e);
  54. // if mouse button is not pressed we have no drag operation, ...
  55. if (e.LeftButton != MouseButtonState.Pressed)
  56. this.rubberbandSelectionStartPoint = null;
  57. // ... but if mouse button is pressed and start
  58. // point value is set we do have one
  59. if (this.rubberbandSelectionStartPoint.HasValue)
  60. {
  61. // create rubberband adorner
  62. AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
  63. if (adornerLayer != null)
  64. {
  65. RubberbandAdorner adorner = new RubberbandAdorner(this,rubberbandSelectionStartPoint);
  66. if (adorner != null)
  67. {
  68. adornerLayer.Add(adorner);
  69. }
  70. }
  71. }
  72. e.Handled = true;
  73. }
  74. protected override void OnDrop(DragEventArgs e)
  75. {
  76. base.OnDrop(e);
  77. DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
  78. if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml))
  79. {
  80. NodeButton newItem = null;
  81. Object content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));
  82. if (content != null)
  83. {
  84. newItem = new NodeButton(dragObject.text,dragObject.tag);
  85. //newItem.Content = content;
  86. Point position = e.GetPosition(this);
  87. if (dragObject.DesiredSize.HasValue)
  88. {
  89. Size desiredSize = dragObject.DesiredSize.Value;
  90. newItem.Width = desiredSize.Width;
  91. newItem.Height = desiredSize.Height;
  92. DesignerCanvas.SetLeft(newItem,Math.Max(0,position.X - newItem.Width / 2));
  93. DesignerCanvas.SetTop(newItem,Math.Max(0,position.Y - newItem.Height / 2));
  94. }
  95. else
  96. {
  97. DesignerCanvas.SetLeft(newItem,Math.Max(0,position.X));
  98. DesignerCanvas.SetTop(newItem,Math.Max(0,position.Y));
  99. }
  100. this.Children.Add(newItem);
  101. //update selection
  102. //foreach (ISelectable item in this.SelectedItems)
  103. // item.IsSelected = false;
  104. //SelectedItems.Clear();
  105. //newItem.IsSelected = true;
  106. //this.SelectedItems.Add(newItem);
  107. }
  108. e.Handled = true;
  109. }
  110. }
  111. protected override Size MeasureOverride(Size constraint)
  112. {
  113. Size size = new Size();
  114. foreach (UIElement element in base.Children)
  115. {
  116. double left = Canvas.GetLeft(element);
  117. double top = Canvas.GetTop(element);
  118. left = double.IsNaN(left) ? 0 : left;
  119. top = double.IsNaN(top) ? 0 : top;
  120. //measure desired size for each child
  121. element.Measure(constraint);
  122. Size desiredSize = element.DesiredSize;
  123. if (!double.IsNaN(desiredSize.Width) && !double.IsNaN(desiredSize.Height))
  124. {
  125. size.Width = Math.Max(size.Width,left + desiredSize.Width);
  126. size.Height = Math.Max(size.Height,top + desiredSize.Height);
  127. }
  128. }
  129. // add margin
  130. size.Width += 10;
  131. size.Height += 10;
  132. return size;
  133. }
  134. }
  135. }