开发者

Visible elements in a WPF canvas

开发者 https://www.devze.com 2023-04-06 17:38 出处:网络
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 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.
         }
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号