Sam
Sam

Reputation: 943

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.

Upvotes: 3

Views: 961

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

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

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564413

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.

Upvotes: 2

Related Questions