1 namespace Microsoft
.Samples
.PerFrameAnimations
4 using System
.Collections
;
5 using System
.Collections
.Generic
;
6 using System
.ComponentModel
;
7 using System
.Diagnostics
;
8 using System
.Windows
.Threading
;
9 using System
.Windows
.Media
;
10 using System
.Windows
.Markup
;
12 using System
.Windows
.Controls
;
15 /// This class is used internally to delegate it's rendering to the parent OverlayRenderDecorator.
17 internal class OverlayRenderDecoratorOverlayVisual
: DrawingVisual
19 private bool _hitTestVisible
= false;
21 internal bool IsHitTestVisible
25 return _hitTestVisible
;
29 _hitTestVisible
= value;
33 //dont hit test, these are just overlay graphics
34 protected override GeometryHitTestResult
HitTestCore(GeometryHitTestParameters hitTestParameters
)
37 return base.HitTestCore(hitTestParameters
);
42 //dont hit test, these are just overlay graphics
43 protected override HitTestResult
HitTestCore(PointHitTestParameters hitTestParameters
)
46 return base.HitTestCore(hitTestParameters
);
55 /// OverlayRenderDecorator provides a simple overlay graphics mechanism similar
56 /// to OnRender called OnOverlayRender
58 [ContentProperty("Child")]
59 public class OverlayRenderDecorator
: FrameworkElement
61 //the only logical child
62 private UIElement _child
;
63 private VisualCollection _vc
;
65 //this will be a visual child, but not a logical child. as the last child, it can 'overlay' graphics
66 // by calling back to us with OnOverlayRender
67 private OverlayRenderDecoratorOverlayVisual _overlayVisual
;
70 //-------------------------------------------------------------------
74 //-------------------------------------------------------------------
79 /// Default constructor
81 public OverlayRenderDecorator()
84 _overlayVisual
= new OverlayRenderDecoratorOverlayVisual();
86 _vc
= new VisualCollection(this);
88 //insert the overlay element into the visual tree
89 _vc
.Add(_overlayVisual
);
95 //-------------------------------------------------------------------
99 //-------------------------------------------------------------------
101 #region Public Properties
104 /// Enables/Disables hit testing on the overlay visual
106 public bool IsOverlayHitTestVisible
110 if (_overlayVisual
!= null)
111 return _overlayVisual
.IsHitTestVisible
;
117 if (_overlayVisual
!= null)
118 _overlayVisual
.IsHitTestVisible
= value;
123 /// The single child of an <see cref="System.Windows.Media.Animation.OverlayRenderDecorator" />
126 public virtual UIElement Child
136 //need to remove old element from logical tree
139 OnDetachChild(_child
);
140 RemoveLogicalChild(_child
);
150 AddLogicalChild(value);
153 //always add the overlay child back into the visual tree if its set
154 if (_overlayVisual
!= null)
155 _vc
.Add(_overlayVisual
);
157 //cache the new child
160 //notify derived types of the new child
162 OnAttachChild(_child
);
170 /// Returns enumerator to logical children.
172 protected override IEnumerator LogicalChildren
178 return (IEnumerator
)new List
<UIElement
>().GetEnumerator();
180 List
<UIElement
> l
= new List
<UIElement
>();
182 return (IEnumerator
)l
.GetEnumerator();
189 //-------------------------------------------------------------------
193 //-------------------------------------------------------------------
195 #region Protected Methods
198 /// Updates DesiredSize of the OverlayRenderDecorator. Called by parent UIElement. This is the first pass of layout.
201 /// OverlayRenderDecorator determines a desired size it needs from the child's sizing properties, margin, and requested size.
203 /// <param name="constraint">Constraint size is an "upper limit" that the return value should not exceed.</param>
204 /// <returns>The OverlayRenderDecorator's desired size.</returns>
205 protected override System
.Windows
.Size
MeasureOverride(System
.Windows
.Size constraint
)
207 UIElement child
= Child
;
210 child
.Measure(constraint
);
211 return (child
.DesiredSize
);
213 return (new System
.Windows
.Size());
217 /// OverlayRenderDecorator computes the position of its single child inside child's Margin and calls Arrange
220 /// <param name="arrangeSize">Size the OverlayRenderDecorator will assume.</param>
221 protected override System
.Windows
.Size
ArrangeOverride(System
.Windows
.Size arrangeSize
)
223 UIElement child
= Child
;
226 child
.Arrange(new Rect(arrangeSize
));
229 //Our OnRender gets called in Arrange, but
230 // we dont have access to that, so update the
232 if (_overlayVisual
!= null)
234 using (DrawingContext dc
= _overlayVisual
.RenderOpen())
236 //delegate to derived types
241 return (arrangeSize
);
245 /// render method for overlay graphics.
247 /// <param name="dc"></param>
248 protected virtual void OnOverlayRender(DrawingContext dc
)
254 /// gives derives types a simple way to respond to a new child being added
256 /// <param name="child"></param>
257 protected virtual void OnAttachChild(UIElement child
)
263 /// gives derives types a simple way to respond to a child being removed
265 /// <param name="child"></param>
266 protected virtual void OnDetachChild(UIElement child
)
271 protected override int VisualChildrenCount
273 get {return _vc.Count;}
276 protected override Visual
GetVisualChild(int index
)