Visible elements in a WPF canvas
I have a WPF Canvas and a lot of Shapes (StreamGeometry / Path) added to it. I have ScaleTransform defined to zoom into specific region.
I have zoomed into a arbitrary space开发者_开发知识库 in the canvas and the Shapes are scaled. Now, is it possible to get the Shapes that are in the visible region of the Canvas.
Thanks for any pointers.
You can use HitTest to perform a hit test against the Canvas's bounding rectangle. For details, see Hit Testing in the Visual Layer and refer to the sample for hit testing with DrawingVisuals.
Should this help?
Iterate thru all children shapes of canvas and check the following for each myShape ....
hitArea
= new EllipseGeometry(
new Point(Canvas.GetLeft(myShape), Canvas.GetTop(myShape)),
1.0,
1.0);
VisualTreeHelper.HitTest(
myShape, null,
new HitTestResultCallback(HitTestCallback),
new GeometryHitTestParameters(hitArea));
public HitTestResultBehavior HitTestCallback(HitTestResult result)
{
if (result.VisualHit == myShape)
{
//// This shape is on the visible area.
}
}
精彩评论