WPF hit testing (and preventing it)

A

Adam Clauss

We have a canvas on which we have implemented the ability for the user
to click and drag to "pan" the view. When the mouse button clicks, we
capture the mouse and as mouse move events occur, we apply a transform
to the canvas to cause the pan effect.

On this canvas, we have hundreds (maybe thousands) of shapes. Lately,
we have begun seeing a performance hit during the panning (panning gets
very choppy). During the pan, one CPU core is effectively maxed out. I
used the Visual Studio profiler to see where we were spending our time.
60% of all samples taken were in a chain of "hit test" methods within
the WPF. I assume this is performing hit testing on all our shapes as
the mouse moves. Is there anyway to prevent this? I have tried setting
IsHitTestVisible on our various shapes - that dropped it to 30%, which
is better, but is STILL a decent chunk of time doing something we do not
want.

Incidentally, we generally do want hit testing - (all these shapes are
'click-able'), just not during a pan operation. During a pan operation,
we know the operation did not start "on" a shape, and since we are
panning the canvas with the mouse movement, we know that the mouse will
not be on top of a shape until some time AFTER the pan operation
finishes. Any ideas on how we might accomplish this, especially since
IsHitTestVisible did not seem to quite do enough to really shortcut the
hittesting process?

Thanks,
Adam
 
R

RayLopez99

finishes.  Any ideas on how we might accomplish this, especially since
IsHitTestVisible did not seem to quite do enough to really shortcut the
hittesting process?

You can cut down on the hits by specifying that the entire shapes be
inside rubber rectangle (what you call "pan"), rather than any portion
of the shapes. There's a property "Geometry" to do this--as an Enum.
Reply here if you can't find it, it's quite conventional so I'm sure
you'll find it.

I'm not sure this will work but it will reduce the number of hits
dramatically, since the rubber rectangle has to encircle (or include)
all of the shape, not just a portion of it, for a hit to be
registered.

That's just my guess. My second guess is rewrite your code from
scratch.

Good luck and let us know how it works out.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top